Forum Moderators: coopster

Message Too Old, No Replies

Pattern Help - Preg Match

help to create a pattern for the preg match function

         

jcheri

5:09 pm on Feb 2, 2009 (gmt 0)

10+ Year Member



hi guyz,

I am a begginner in PHP, and I don't understand the syntax of regular expressions. Can anyone help me !

Site produces - this

&nbsp;<a href='/s/s/s/s.10057/'>Name (YYYY)</a> 
&nbsp;<a href="something" onclick="something" onfocus="this.blur()"></a>
&nbsp;<a href="something" onclick="something" onfocus="this.blur()"></a>
&nbsp;<a href='/s/s/s/s.8000/'>Name2 (YYYY)</a>
&nbsp;<a href="something2" onclick="something2" onfocus="this.blur()"></a>
&nbsp;<a href="something2" onclick="something2" onfocus="this.blur()"></a>

What I want is that the number inside the href (10057) and the text "Name (YYYY)". And I tryed many examples but because of the fact "/s/s/s/" seem to change and the number is with a word.

Please anyone help !

Little_G

5:25 pm on Feb 2, 2009 (gmt 0)

10+ Year Member



Hi,

Try:

/<a href=[\"']\/\w\/\w\/\w\/\w\.(\d+)\/[\"']>([^<]+)<\/a>/

Andrew

jcheri

11:02 pm on Feb 2, 2009 (gmt 0)

10+ Year Member



Hi again,

thanx for the rapid reply, but now I am kind of confused. Let me explain more of what I want, I want the number (from href), Name and the Year to be saved into the database (mysql). To do that I thought of having arrays then looping it to insert the items into the database. But i am not a fan of regular expressions, Can you give a brief info on what I need to do to succeed !?

Here is what I thought;

using preg_match_all make arrays on the number, name and the year - eliminating the other codes around then insert it to database.

What you think! (please give code examples - thank you)

coopster

12:54 pm on Feb 3, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, jcheri.

Your logic sounds correct, now just apply the regular expression pattern to your string. You may be able to shorten that pattern if all you are after is the digits at the end followed by the path separator.

$pattern = '/<a href=["\'][^>]+\.(\d+)\/["\']>([^<]+)<\/a>/s'; 
preg_match_all($pattern, $subject, $matches);
print '<pre>';
print_r($matches);
print '</pre>';
exit;

View the source of the output above in your browser and you will notice how the $matches array has captured your data. You can then loop over that multidimensional array to grab the pieces you need for database insertion. Modify the pattern to meet your needs.