0

I'm trying to extract value of each html tag from my string into array. This is my str value:

str = '<li><a href="/somelink1">name1</a></li><li><a href="/somelink2">name2</a></li><li><a href="/somelink3">name3</a></li>'

I want to extract each name (name1, name2, name3) and put it into array. So the output should be:

output = Array(
           [0] => 'name1',
           [1] => 'name2',
           [2] => 'name3'
         )

I was looking for some function that may do something similar, but no luck at all :/

4 Answers 4

5

You can use html parser for that

$dom = new DomDocument();
$dom->loadHTML($str);

$lis = [];
foreach($dom->getElementsByTagName('li') as $li) {
    $lis[] =  $li->nodeValue;
}

print_r($lis); // Array ( [0] => name1 [1] => name2 [2] => name3 )
Sign up to request clarification or add additional context in comments.

1 Comment

That's what I was looking for! Thank you very much, working as expected.
2

There are multiple ways to do it and regex is one of them. You can try like this way with preg_match_all()

<?php
$re = '/<a ?.*?>([^<]+)<\/a>/m';
$str = '<li><a href="/somelink1">name1</a></li><li><a href="/somelink2">name2</a></li><li><a href="/somelink3">name3</a></li>';
$result = [];
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
foreach($matches as $match){
    $result[] = $match[1];
}
print '<pre>';
print_r($result);

Output:

Array ( 
  [0] => name1 
  [1] => name2 
  [2] => name3 
)

DEMO: https://3v4l.org/B7k58

REGEX: https://regex101.com/r/HPDhtA/1

2 Comments

Thank you, I was trying with regex before but I could not get it, your solution also works but I see @splash58 solution as more clear.
Ok @Steve glat it helps you somehow. By the way see stackoverflow.com/help/someone-answers
0

Actually @splash58 answer is in right direction but he might miss the actual requirement i.e. only Name value not links, if I'm not wrong. So just try this

$dom = new DomDocument();
$dom->loadHTML($str);

$names = [];
foreach($dom->getElementsByTagName('a') as $a) {
    $names[] =  $a->nodeValue;
}

print_r($names);

1 Comment

Hi, thank you, I was looking only for names of those links (<a>).
0

You could also make use of an xpath expression using DOMXPath and specify exactly what you want to find:

//li/a

For example:

$str = '<li><a href="/somelink1">name1</a></li><li><a href="/somelink2">name2</a></li><li><a href="/somelink3">name3</a></li>';
$dom = new DomDocument();
$dom->loadHTML($str);
$xpath = new DOMXPath($dom);
$result = [];
foreach($xpath->evaluate('//li/a') as $a) {
    $result[] = $a->nodeValue;
}

print_r($result);

Result

Array
(
    [0] => name1
    [1] => name2
    [2] => name3
)

Comments

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.