Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ereg_replace: pass substring to function? 1

Status
Not open for further replies.

OsakaWebbie

Programmer
Feb 11, 2003
628
JP
When using ereg_replace, is it possible to call a function to operate on the "\\1" substring somehow? If I just say:
Code:
echo ereg_replace("start(middle)end","start".myfunction("\\1")."end",$thestring)
it passes a literal backslash and a literal "1", rather than the string that \\1 should represent. If I leave off the quotes, I get two warnings about a backslash, and a literal "1" is passed to the function each time. Encasing it in { and } doesn't help. Is there a technique that will work, or do I have to make a loop to dismantle my string, call my function, and put the string back together again? There is no mention in the PHP docs of using function calls with ereg_replace, so no clues there.
 
Well, I admit that I don't understand what the single quotes inside the double quotes do, and why I can still go in and out of a quoted literal string inside that /.../e construct (my first example was much simpler than my real code) - ages ago I dabbled in Perl a little bit, but not enough to know how to do such fancy regexp stuff. But once I got all the punctuation in the right place, it worked! My final version (with my real code this time, so you can see what I was up against) is:
Code:
preg_replace("/<p class=\"?".$lyrics_class."\"?>([^<]*\[[^<]*)<\/p>/e","'<p class=\"chordlyrics\">'.chordsToRuby('$1').'</p>'",$song[$item_index])
Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top