preg_split help needed

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
maxjac
Forum Newbie
Posts: 2
Joined: Mon Mar 27, 2006 10:19 am

preg_split help needed

Post by maxjac »

I have the following dynamic string:

Jacques, P</div><div class="text" style="line-height:13pt;">2789 Henri Cyr<br>Saint Hubert, QC J0Y 5N6

I need to split it to the different dynamic parts that I highlighted in bold:

-> Jacques
-> P
-> 2789
-> Henri Cyr
-> Saint Hubert
-> QC
-> J0Y 5N6

The HTML code in the string will always be there and therefore can be used as the delimiters for the preg_split function but I was never able to get it to work.

Thanks for your time and help is greatly appreciated.
Maxjac.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Jacques, P</div><div class="text" style="line-height:13pt;">2789 Henri Cyr<br>Saint Hubert, QC J0Y 5N6

Code: Select all

preg_match('@>(\w+), (\w+)</div><div class="text" style="line-height:13pt;">(\d+) ([\w ]+)<br>([\w ]+), ([A-Z]+) ([A-Z0-9 ]+)@is', $string, $matches);

print_r($matches);
maxjac
Forum Newbie
Posts: 2
Joined: Mon Mar 27, 2006 10:19 am

Post by maxjac »

Hello and thanks for you help d11wtq !

I have tried what you suggested but the output is

Array ()

I have pasted the code from your reply and changed '$string' to my variable name...
I even changed my variable to the exact text I had posted on the forum to make sure it's not my code that changes something...

Code: Select all

$myLine = 'Jacques, P</div><div class="text" style="line-height:13pt;">2077 Henri Cyr<br>Saint Hubert, QC  J3Y 8N6';

preg_match('@>(\w+), (\w+)</div><div class="text" style="line-height:13pt;">(\d+) ([\w ]+)<br>([\w ]+), ([A-Z]+) ([A-Z0-9 ]+)@is', $myLine, $matches);

print_r($matches);
What could be the problem ?

Thanks again :)
Maxjac
Post Reply