Why won't this simple PHP code not work?
-
Regarding Preg_Replace function in PHP Greetings. I am trying to use the Preg_Replace function to replace a string variable with another string. This works fine when the string I'm looking for is written out, such as "This is my string." However, when I try to use the dollar sign to use a normal string, such as $This_is_my_string, I keep getting error messages about non-alphanumeric delimiters. I've tried every syntax I can think of, but I still get the error message: Below is an example of the sort of thing I'm trying so far without success: Anyone know how to fix this? Thanks, Brian $find = array ('$words_to_find'); $replace = array ('This is the Replacement String'); $Text = preg_replace ($find, $replace, $Text); I've also tried $find = ('/$words_to_find/'); and $find = ($words_to_find);
-
Answer:
You are trying to replace a string, as such you should use str_replace instead. See: http://php.net/str_replace -----CodeStart----- <?php $string = '$Replace_This'; $withstring = 'This is the Replacement String'; echo str_replace($string, $withstring, $string); ?> -----CodeEnd----- Using an array: -----CodeStart----- <?php $string = '$Replace_This (In Between String) $Replace_This'; $Replace = array('$Replace_This'); $withstring = array('This is the Replacement String'); echo str_replace($Replace, $withstring, $string, $count) . ' '; echo 'Total Replaced variables: ' . $count; ?> -----CodeEnd----- If you for some reason still want to use preg_replace, the problem you where having was that the "$" sign is a special character, and as such it should be escaped with "\". Normally it would work simply to have the string inclosed by single quotes, but since the "Doller Sign" is used to design regular expressions with, we need to escape it. The below will work: -----CodeStart----- <?php $string = '$Replace_This (In Between String) $Replace_This'; $Replace = array('/\$Replace_This/'); $withstring = array('This is the Replacement String'); echo preg_replace($Replace, $withstring, $string) . ' '; ?> -----CodeEnd----- Note, the count parameter is optional, but can be used to return the number of matched patterns, which where replaced. If you are trying to replace usercodes with html equlivants, the following answer may interest you: http://answers.yahoo.com/question/index;_ylt=ApCGeScxN3tbEJl_lHeNzcHty6IX;_ylv=3?qid=20080312181143AAD2oa5&show=7#profile-info-83f19a0528c5c127d942b72c966f472daa
canterbu... at Yahoo! Answers Visit the source
Related Q & A:
- Why won't my @media queries work?Best solution by stackoverflow.com
- Why won't my camcorder work on my computer?Best solution by Yahoo! Answers
- Why won't my CD drive work?Best solution by Yahoo! Answers
- Why won't my camera work on Windows live messenger?Best solution by answers.microsoft.com
- Why won't my Time Warner Cable remote work?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.