0

hi i have a data in below format

<option value="http://www.torontoairportlimoflatrate.com/aurora-limousine-service.html">Aurora</option> <option value="http://www.torontoairportlimoflatrate.com/alexandria-limousine-service.html">Alexandria</option>

i after banging my head on table 10 times figured out to use regular expression below

preg_match_all("#>\w*#",$data,$result);

This returns the results as below

 Array
(
    [0] => Array
        (
            [0] => >Ajax
            [1] => >
            [2] => >Aurora
            [3] => >
            [4] => >Alexandria
            [5] => >
            [6] => >Alliston

I only want single array having values i.e. cities [0] => Ajax [1] => Aurora ...... so on.

Pleas

Khurram Ijaz
  • 1,844
  • 5
  • 24
  • 43
  • 1
    Don't use a regex. Use a HTML parser. See [Best methods to parse HTML](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html) – Pekka Apr 25 '11 at 06:44
  • This is a job for an [HTML Parser](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – jb. Apr 25 '11 at 06:45

1 Answers1

1

If you'd prefer not to use an HTML parser, you can do it with a regex, but keep in mind that you'll probably need to modify it based on what you'll receive as input in the future. For your specific problem, this is a regex that does the job:

<?php
    preg_match_all('/<option\svalue=\"([a-zA-Z0-9-_.\/:]+)\">([a-zA-Z\s]+)<\/option>/', $data, $result);

    var_dump($result[2]);

Note:

If you want to match every url you should replace ([a-zA-Z0-9-_.\/:]+) with a more capable url matching regex. You can find some on StackOverflow also, but for me is a matter of personal taste.

Community
  • 1
  • 1
Bogdan Constantinescu
  • 5,296
  • 4
  • 39
  • 50