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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.