search for something in a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GrAcK
Forum Newbie
Posts: 4
Joined: Fri Jul 11, 2003 12:31 pm
Location: Sweden
Contact:

search for something in a string

Post 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?
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post 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
Judas
Forum Commoner
Posts: 67
Joined: Tue Jun 10, 2003 3:34 pm
Location: Netherlands

Post by Judas »

correct learn it.

:idea:
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.
GrAcK
Forum Newbie
Posts: 4
Joined: Fri Jul 11, 2003 12:31 pm
Location: Sweden
Contact:

Post 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.
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post 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 "://")
GrAcK
Forum Newbie
Posts: 4
Joined: Fri Jul 11, 2003 12:31 pm
Location: Sweden
Contact:

Thanks!

Post 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 :wink:
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

Er... \\0, sorry :)
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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
GrAcK
Forum Newbie
Posts: 4
Joined: Fri Jul 11, 2003 12:31 pm
Location: Sweden
Contact:

Post 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?
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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">
Post Reply