how i can pass an intege variable to preg_replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crishna369
Forum Newbie
Posts: 10
Joined: Mon May 25, 2009 12:35 am

how i can pass an intege variable to preg_replace

Post by crishna369 »

how i can pass a variable containing an integer value to preg_replace.
plz help.
MeLight
Forum Commoner
Posts: 26
Joined: Sun Apr 19, 2009 12:39 pm
Location: Israel

Re: how i can pass an intege variable to preg_replace

Post by MeLight »

Code: Select all

 
$subj = "hello, here are some nums 12384";
$int = 3;
$patt = "/$int/";
echo preg_replace($patt, "<blah>", $subj);
 
Last edited by Benjamin on Wed May 27, 2009 10:00 am, edited 1 time in total.
Reason: Changed code type from text to php.
crishna369
Forum Newbie
Posts: 10
Joined: Mon May 25, 2009 12:35 am

Re: how i can pass an intege variable to preg_replace

Post by crishna369 »

thanx its working!!!!!!!!
crishna369
Forum Newbie
Posts: 10
Joined: Mon May 25, 2009 12:35 am

Re: how i can pass an intege variable to preg_replace

Post by crishna369 »

sorry buddy its not working
its removing the number 3 but not replacing it with the text.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: how i can pass an intege variable to preg_replace

Post 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
MeLight
Forum Commoner
Posts: 26
Joined: Sun Apr 19, 2009 12:39 pm
Location: Israel

Re: how i can pass an intege variable to preg_replace

Post 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.
Post Reply