Need to split and exclude a string - PHP

0 views
Skip to first unread message

Rus Miller

unread,
Jun 20, 2009, 5:20:53 PM6/20/09
to Regex
The pattern of the string will be something like the following:

60003 Lichen<br />Stripe<br />

What I need to do is exclude everything after the product name (in
this case, 'Lichen'). The resulting string should look like:

60003 Lichen



Thank you for your help!

Eugeny Sattler

unread,
Jun 22, 2009, 3:25:19 AM6/22/09
to re...@googlegroups.com
Firstly, you most probably meant </br> and not <br/>
60003 Lichen</br>Stripe</br>


Secondly, lets define approach: you mean the necessary info ends right
in front of the first "</br>", right?

in this case the regex will be
^.*?(?=\<\/br\>)

and, if applied to my reply text, three matching results will be:

"First you probably mean "
"60003 Lichen"
"Secondly, lets define approach: you mean the necessary info ends
right in front of the first ""

A bit of explanation:
^ checks if we are at the start of a line but grabs nothing (it is an anchor)
.*? is a lazy dot, i.e. it grabs anything as few times as possible, so
that the next regex construct finds its match
and finally (?=\<\/br\>) again grabs nothing but checks whether what
we have to the right is "</br>"
Putting a backslash in front of special; symbols is a precaution in
order that they are not treated as special symbols of programming
language/environment

--
Regards,
Eugeny Sattler

Rus Miller

unread,
Jun 22, 2009, 9:42:14 AM6/22/09
to Regex
Thanks for the reply and the explanation. Your solution does work,
although I did mean <br />, which is valid XHTML for self-closing
elements.

http://www.w3schools.com/TAGS/tag_br.asp
Reply all
Reply to author
Forward
0 new messages