Search code examples
phparraysstringsplitexplode

PHP: Split string into array around a wildcard


I have a string containing:

< a href="/wiki/Bob" title="Bob" >

There are ten and more unrelated data in bettween them that i don't need. Basically, I want the title for each one placed in an arrray and they're different. I thought this would be easy, but I can't find out how to do it. Perhaps using explode() and a wildcard, but apparently you can't use a wildcard?

Any help greatly appreciated.

EDIT:I forgot to mention that the will change every week form 'Bob' for example to 'Tim'.


Solution

  • Maybe something like this?

    <?php
    $string = '<th align="center" style="width:10%;"><a href="/wiki/Bob" title="Bob"><img alt="BobSquare.png" src="image" width="48" height="48" /></a><br /><a href="/wiki/Bob" title="Bob" class="mw-redirect">Bob</a>';
    
    $title_array = array();
    $explode = explode('title="', $string);
    unset($title_array[0]);
    foreach($title_array as $k => $v)
    {
        $explode_string = explode('"', $v);
        $title_array[] = $explode_string[0];
    }
    
    print_r($title_array);
    ?>
    

    Output:

    Array
    (
        [0] => Bob
        [1] => Bob
    )