CoderZone.org
Pages: 1 « previous     next »
  Print  
Author Topic: Converting eregi to preg_replace....  (Read 5693 times) Bookmark and Share
Cruise Elroy
Newbie
*
Posts: 14



View Profile
« on: Feb 18, 2011, 06:59:11 pm »

Is there anyone familiar enough with preg_replace to be able to convert this from the eregi_replace to preg_replace()? I've tried about 50 ways and still haven't gotten it right.

Code:  
Highlight Mode: (PHP)
  1. $some_text = eregi_replace("([[:space:]()[{}])($word)", "\\1<a href='$rep_string' >\\2</a>", $some_text);
 
Logged
wheels
Jr. Member
*****
Posts: 62



View Profile
« Reply #1 on: Feb 20, 2011, 06:43:58 am »

You might be able to use str_replace.

Code:  
Highlight Mode: (PHP)
  1. <?php
  2.  
  3. $word='cat';
  4. $rep_string='catz.com';
  5.  
  6. $string='The cat likes to sleep';
  7. $rep_link='<a href="'.$rep_string.'">'.$word.'</a>';
  8.  
  9. $some_text=str_replace($word,$rep_link,$string);
  10.  
  11. echo $some_text.PHP_EOL;
 

Output

Quote
The <a href="catz.com">cat</a> likes to sleep

It could also be str_ireplace, and a count can be set to limit the number of replacements.
« Last Edit: Feb 20, 2011, 05:14:38 pm by wheels » Logged
phpMan2010
Newbie
*
Posts: 32



View Profile
« Reply #2 on: Feb 21, 2011, 05:45:58 am »

... or this ...

Code:  
Highlight Mode: (PHP)
  1. <?php
  2.  
  3. $some_text='The Cat likes to sleep';
  4. $word='cat';
  5. $rep_string='http://catz.com';
  6.  
  7. $some_text = eregi_replace("([[:space:]()[{}])($word)", "\\1<a href='$rep_string'>\\2</a>", $some_text);
  8.  
  9. echo $some_text.PHP_EOL;
  10.  
  11. $some_text = preg_replace('/(\s+)('.preg_quote($word).')/i','$1<a href="'.$rep_string.'">$2</a>',$some_text);
  12.  
  13. echo $some_text.PHP_EOL;
 

Logged
Cruise Elroy
Newbie
*
Posts: 14



View Profile
« Reply #3 on: Feb 21, 2011, 10:55:42 am »

Awesome, phpMan, I'll give this a shot. Thank you!

EDIT: Works perfectly. Thank you!!

Code:  
Highlight Mode: (PHP)
  1. <?php
  2. $some_text = preg_replace('/(\s+)('.preg_quote($word).')/i','$1<a href="'.$rep_string.'">$2</a>',$some_text);
  3.  
  4. echo $some_text.PHP_EOL;
 
« Last Edit: Feb 21, 2011, 11:16:53 am by Cruise Elroy » Logged
Tags:
Pages: 1
  Print  
 
Jump to: