Jump to content

ereg_replace + function?


mrberti

Recommended Posts

Hello,

I'm attempting to pass a user-defined function in an ereg_replace, but having difficulties.

 

$text = "blah 9 blah";
echo eregi_replace("([1-9])", doSomething('\\1'), $text);

function doSomething($i) {
echo $i."<br />";
if($i == "9") return "foo";
else return "~$i~";
}

 

outputs...

\1
blah ~9~ blah

 

So in doSomething(), $i goes through correctly on the RETURN, but not on the ECHO or IF... I guess I am handling $i incorrectly?

Any help is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/42194-ereg_replace-function/
Share on other sites

Thanks! I took your code and applied to perfectly to my script, but I encountered another problem. How do you do an 'except' clause in preg? For example, I would like to do "match any single character (letter, number, special character, or otherwise.. anything) except Z"?

Link to comment
https://forums.phpfreaks.com/topic/42194-ereg_replace-function/#findComment-204888
Share on other sites

OK... I'm trying to find and replace links in text. for example...

 

The [[G||Chrono Trigger]] soundtrack proved extremely popular with fans. Mitsuda worked on four more titles for Squaresoft, the last being [[G||Xenogears]] in 1998.

 

I would like to find, analyze, and replace [[G||...]] with a link. So how would you do a preg match? So far I have:

/\[\[G\|\|([- a-z0-9]+)\]\]/ise

but I want to make an expression that says "match ANY character within the brackets" (including special and foreign characters that may be there).

 

Thanks again.

Link to comment
https://forums.phpfreaks.com/topic/42194-ereg_replace-function/#findComment-207661
Share on other sites

<pre>
<?php

$string = 'The [[G||Chrono Trigger]] soundtrack proved extremely popular with fans. Mitsuda worked on four more titles for Squaresoft, the last being [[G||Xenogears]] in 1998.';
preg_match_all('/(?<=\[\[G\|\|)([^]]+)(?=\]\])/', $string, $matches);
print_r($matches);

?>
</pre>

Link to comment
https://forums.phpfreaks.com/topic/42194-ereg_replace-function/#findComment-207679
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.