News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

preg_replae e and s

Started by Tyris, October 07, 2003, 08:59:02 PM

Previous topic - Next topic

Tyris

ok, basically... code:
$content = preg_replace('/<some tag>(.+?)<\/some tag>/e', somefunction('\\1'), $content
works (I think) but only if the tags are on one line
$content = preg_replace('/<some tag>(.+?)<\/some tag>/si', somefunction('\\1'), $content
sends the value "\1" to the function (so doesn't work)
$content = preg_replace('/<some tag>(.+?)<\/some tag>/sie', somefunction('\\1'), $content
gets the following error:
Parse error: parse error in c:\apache\htdocs\jubilant jow\index.php(18) : regexp code on line 1

Fatal error: Failed evaluating code:  in c:\apache\htdocs\jubilant jow\index.php on line 18

basically I cant seem to combine the si and e commands... which is a problem... I can change it all and expand it... but I'd rather making it small like that...

any help appreciated ^_^
Thanx

[Unknown]

Should be:

$content = html_entity_decode(preg_replace('~&lt;some tag&gt;(.+?)&lt;/some tag&gt;~eis', "somefunction('\$1')", htmlspecialchars($content)));

I think.  The issue is 's in the match...

-[Unknown]

Tyris

#2
ok, sweet thanx!!
I'll try it... I dont entirely understand it ;) but ... :D
whats the purpose of ~ bits...? as apposed to / ??

Thanx again!!! :)

*edit* looks like its time to download an update ;) html_entity_decode wasnt recognised (I'm on php4.1.1 ... an old phptriad install)
*edit 2*
I read up on those two html commands... and as is obvious, they convert the html code into the 'special' code (whatever its name is...)
I actually need it in html form... so the following is working for me :)
$content = preg_replace('~<some tag>(.+?)</some tag>~eis', "somefunction('\$1')", $content);
Thanx again!! :)
(tho if you can tell me what the diff of using ~ is then plz reply :))

[Unknown]

There's no difference really.  It's just that I found it annoying to slash the closing tag.  (/\// compared to ~/~)  All you have to use is to characters on the sides - you can use //, [], (), whatever.  I'm quite fond of tildas ;). (well, and brackets sometimes of course ;D.)

-[Unknown]

Tyris

#4
ok, cool, I agree it looks better :)
tildas? is that its official name...? I call em squigles ;D
I guess then my problem was in the calling of 'somefunction' then...?
coz other than using ~ the pattern is no different...

also... whilst on the topic of preg_replace...
how would I go about only selecting those tags that are one level deep...?
ie: so I can embed the tags...
eg:
<some tag>
   <some tag>
   </some tag>
</some tag>

how would I make it select the outer most case of the tag first...
I know it can be done using a long series of str_pos commands to find the opening and ending ones... but I'm confident there'll be a way to do it with preg_replace...
*edit: It'd be fine to start from the inside too... its just obviously a problem if it selects everything from the first opening tag to the first closing tag... I was also thinking of just making each tag numbered... but thats kinda silly =\*
no doubt its somewhat complicated and long, so if you could direct me to a turorial instead I'd be greatly obliged.
I doubt I'll need embedding for what I'm doing... but it'd be nice just in case... :)

heheh, [] brackets are great too ;D

Advertisement: