-1

Some links like these:

[links url='http://www.google.com.hk' title='Google' image='']google[/links]
[links url='http://hk.yahoo.com' title='yahoo' image='']yahoo[/links]

how to use PHP Regular expression get the url attributes? Thanks.

http://www.google.com.hk
http://hk.yahoo.com
fish man
  • 2,666
  • 21
  • 54
  • 94
  • 2
    May I suggest experimenting in something like http://regexpal.com/ – Doug T. Aug 23 '11 at 22:44
  • @Doug T. , no, the url not guaranteed, these just in long text, and I only want to get the `url` attributes? – fish man Aug 23 '11 at 22:44
  • @Doug T. I only know simple dom, but this are not regular links. so need for a help. – fish man Aug 23 '11 at 22:46
  • @fish If you don't understand regex, getting someone to do it for you won't help you learn. – adlawson Aug 23 '11 at 22:48
  • Might want to integrate this post into preg_match_all: [http://stackoverflow.com/questions/1141848/regex-to-match-url/1141962#1141962][1] [1]: http://stackoverflow.com/questions/1141848/regex-to-match-url/1141962#1141962 – Paweł Adamski Aug 23 '11 at 22:58
  • @adlawson sometimes a few good, practical, well-explained examples will help someone learn. – Jacob Eggers Aug 23 '11 at 23:02

2 Answers2

2

This should get you started:

preg_match_all("/\[links[^\]]+url='([^']+)'/", '{{your data}}', $arr, PREG_PATTERN_ORDER);

Explanation of the regex:

/
\[                  //An excaped "[" to make it literal.
links               //The work "links"   
[^\]]+              //1+ non-closing brackent chars ([^] is a negative character class)
url='               //The work url='
([^']+)             //The contents inside the '' in a caputuring group
/               
Jacob Eggers
  • 9,062
  • 2
  • 25
  • 43
1

Use this regex: /\[links\s+(?:[^\]]*\s+)*url=\'([^\']*)\'[^\]]*?\]/

$str = "[links url='http://www.google.com.hk' title='Google' image='']google[/links]";
$m = array();
preg_match('/\[links\s+(?:[^\]]*\s+)*url=\'([^\']*)\'[^\]]*?\]/', $str, $m);
echo $m[1];
Paul
  • 139,544
  • 27
  • 275
  • 264
  • Actually, that won't work if url isn't first. Try it against: `[links title='Google' url='http://www.google.com.hk' image='']google[/links]` – Jacob Eggers Aug 23 '11 at 23:20
  • @Jacob You are correct, I forgot a `*`. I fixed it now so that order doesn't matter. – Paul Aug 23 '11 at 23:26