Page 1 of 1
search for something in a string
Posted: Fri Jul 11, 2003 12:31 pm
by GrAcK
I wonder how I can search for some thing in a text string for example: how do I search for all <IMG> taggs? Because the <IMG>-tag contains diffrent things each time I can't use the stristr() function.
Anybody got a suggestion?
Posted: Fri Jul 11, 2003 1:00 pm
by qartis
Regular Expressions are what you want. I don't want to write you one, because
a) You wouldn't learn
b) I really suck at writing regexes
Functions are at
http://ww.php.net/regex
Posted: Fri Jul 11, 2003 1:30 pm
by Judas
correct learn it.
btw before launching a ereg func. explode the string in an array.
So when you are lucky, you should have an array with al tag's from the file, then you can explode more or find your repl. string.
Posted: Fri Jul 11, 2003 1:37 pm
by GrAcK
I found this example on the page posted.
Code: Select all
<?php
$text = ereg_replace("[[]]+://[^<>[]]+[[]/]",
"<a href="\\0">\\0</a>", $text);
?>
it just what i want. BUT, there is one thing. i understand that where
\\0 som text is posted, but i dont see where it is declared what text will be showed where the
\\0 is.
Posted: Fri Jul 11, 2003 2:52 pm
by qartis
IIRC, //0, //1 et cetera are the matched strings, from the [[:boxed:]] stuff, in order. In your example, //0 would equal the protocol of the url (the [[:alpha:]] match - any alphanumeric characters before "://")
Thanks!
Posted: Fri Jul 11, 2003 5:33 pm
by GrAcK
Thanks alot! Now my page has got better. Thanks to you guys!
I will have a closer look at the regular expressions. they seems intresting

Posted: Sat Jul 12, 2003 6:20 am
by qartis
Er... \\0, sorry

Posted: Sat Jul 12, 2003 9:46 am
by m3rajk
ewwwwwwwwwwwwww!!!!!!!!!!!!!! don't use ereg here!!!!!!!
ere is posix and therefore a forced greedy. there are times that will backfire. you need perl so you can do non-greedy.
trust me, i already went through finding it out on my own:
http://people.brandeis.edu/~m3rajk/JMT/ ... c-fcv4.php
Posted: Sat Jul 12, 2003 11:21 am
by GrAcK
sorry, im a bit of a newbie so what do you mean by force greedy? and perl, i know it's a language but can I use it combined with php? and do my server need to support perl?
Posted: Sat Jul 12, 2003 1:19 pm
by m3rajk
php has functions that use the perl regular expressions instead of the posix ones.
by greedy i mean that if you have someone accidentally type:
blah blah<img src="url">blah blah >and some more stuff
then it will pick up: <img src="url">blah blah >
perl has a non-greedy method. using the preg_replace instead of ereg_replace you can specify non-greedy so it will then correctly pick up: <img src="url">