Page 1 of 1

how i can pass an intege variable to preg_replace

Posted: Wed May 27, 2009 5:53 am
by crishna369
how i can pass a variable containing an integer value to preg_replace.
plz help.

Re: how i can pass an intege variable to preg_replace

Posted: Wed May 27, 2009 6:18 am
by MeLight

Code: Select all

 
$subj = "hello, here are some nums 12384";
$int = 3;
$patt = "/$int/";
echo preg_replace($patt, "<blah>", $subj);
 

Re: how i can pass an intege variable to preg_replace

Posted: Wed May 27, 2009 7:38 am
by crishna369
thanx its working!!!!!!!!

Re: how i can pass an intege variable to preg_replace

Posted: Wed May 27, 2009 7:51 am
by crishna369
sorry buddy its not working
its removing the number 3 but not replacing it with the text.

Re: how i can pass an intege variable to preg_replace

Posted: Wed May 27, 2009 10:01 am
by Benjamin
Forum Rules wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.
You may also want to read:
  1. General Posting Guidelines
  2. Posting Code in the Forums
  3. PHP Manual
  4. PHP Tutorials

Re: how i can pass an intege variable to preg_replace

Posted: Wed May 27, 2009 2:45 pm
by MeLight
It work on my machine. Do you have any other preg_replace commands afterwards? If you're removing tags, the "<blah>" will be removed also. try this:

Code: Select all

 
$subj = "hello, here are some nums 12384";
$int = 3;
$patt = "/$int/";
echo preg_replace($patt, "three", $subj);   //"<blah>" replaced with "three"
 
If it still doesn't work, post your code.