0

I have a string like this

<a href="http://www.example1.com"><b>12345</b> - John George<br><span>some_text1</span></a>
<a href="http://www.example2.com"><b>67890</b> - George Jerry<br><span>some_text2</span></a>

Using preg_match_all (PHP) I want to be able to extract the url, id and name but I`m not figured out the good sPattern (see bellow):

$sPattern = "/<a href=\"(.*?)\"><b>(.*?)<\/b>\" - (.*?)\"<br>(.*?)/";
preg_match_all($sPattern, $content, $aMatch);

2 Answers 2

2

I humbly suggest use an HTML Parser like DOMDocument instead:

$html = '<a href="http://www.example1.com"><b>12345</b> - John George<br><span>some_text1</span></a>
<a href="http://www.example2.com"><b>67890</b> - George Jerry<br><span>some_text2</span></a>';

$dom = new DOMDocument();
$dom->loadHTML($html);
$anchors = $dom->getElementsByTagName('a');
$data = array();
foreach($anchors as $anchor) {
    $href = $anchor->nodeValue; // get the anchor href
    $b = $anchor->firstChild->nodeValue; // get the b tag value
    $data[] = array('href' => $href, 'id' => $b);
}

echo '<pre>';
print_r($data);
Sign up to request clarification or add additional context in comments.

Comments

1

Probably better if you write a bit more specific patterns, try this one:

$sPattern = "/<a href=\"([ˆ"]+)\"><b>(\d+)<\/b> - ((\w+ )*\w+)<br><span>([^<]+)<\/span><\/a>/";

2 Comments

I tried a pattern like this: "/<a href=\"(.*?)\"><b>(.*?)<\/b> - (\w+)/" It works fine but with a little exception: if I have names like 'Tom-Jerry' or 'Tom & Jerry' returns only the first word (e.g. 'Tom')
I see, then you have to change the last bracket, probably the best way to change it is to ([^<]+). That would select everything to the </span> element. The only problem would be if there were another element inside the span, something like <span>Tom & <b>Jerry</b></span>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.