Solutions for the preg_replace function

Source: Internet
Author: User
About the preg_replace function, I want to use the preg_replace function to replace some characters in text with images. when I write this code, $ expression & nbsp; = "/@ /" can be executed normally /"; $ reexpression & nbsp; = ""; $ solve the preg_replace function
I want to use the preg_replace function to replace some characters in text with images.
When I write this code, it can be executed normally.

$ Expression = "/@ fangya /";
$ Reexpression = "fangya ";
$ Text = preg_replace ($ expression, '', $ text );

However, after I change it to an array, the image url becomes www.test.com/static/expression/array.gif.

$ Expression = array ("/@ fangya/", "/@ despise/", "/@ torture /");
$ Reexpression = array ("fangya", "despise", "torture ");
$ Text = preg_replace ($ expression, '', $ text );

I am new to php. I hope you can take a look.
------ Solution --------------------
The parameter value is calculated before the function is executed. So that is:
$ Replace = '';
$ Text = preg_replace ($ expression, $ replace, $ text );
// Because $ reexpression is a number Group, the combination of the number Group and reexpression will be array.gif.

Let's look at your requirements and write it again:
$ Text = 'Don't @ your teeth,-> @ despise <-';
$ Reexpression = array ("fangya", "despise", "torture ");
// Hope your program is better coded in UTF-8, u modifier is to avoid unexpected confusion in coding
$ Find = '# @ ('. join ('
------ Solution --------------------
', $ Reexpression).') # U ';
$ Replace = '';
$ Text = preg_replace ($ find, $ replace, $ text );

In addition, if your image is named in Chinese, you also need to pay attention to the encoding problems that may occur in IE, resulting in a 404 error in image loading.

Attached Manual: http://php.net/preg-replace

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.