0

I tried many function but I am failed to get exact value. Here is my dynamic text. I want to get/extract only Sizes value (36,38,40, ...). How I extract from this text value? This is full content which I have dynamic from the database.

<ul>
<li>
<span>Colors: </span>Off White, Turquoise</li>
<li><span>Type: </span>Stitched</li>
<li><span>Sizes: </span>36, 38, 40</li>
</ul>
Jaydip Satvara
  • 138
  • 1
  • 15
  • 2
    Possible duplicate of [How do you parse and process HTML/XML in PHP?](https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – Stephan Vierkant Jul 09 '18 at 09:47
  • Or `xpath`. But if you get the data from "the database", why are you trying to get it out of a text document? – Loek Jul 09 '18 at 09:47
  • @Loek Is it possible to extract which I want? – Jaydip Satvara Jul 09 '18 at 09:49
  • Please be clearer. You can literally do anything in code, short from creating a time machine. Some things take one line and a minute, some things take 10 gigabytes and twenty years, but literally anything can be programmed. – Loek Jul 09 '18 at 09:51
  • @Loek, Above is one for the strings which I get it from the database dynamically. I need to extract or require only Sizes: 36,38,40 value from it. I don't want extra things on it. – Jaydip Satvara Jul 09 '18 at 09:54

2 Answers2

0

If you want to pull the data directly it would be tough, it's better to convert it into an array then loop through the array to get the value you're looking for.

<?php 
$html = '<li> <a href="link1"> 36 </a> <li>
<li> <a href="link2"> 38 </a> <li>
<li> <a href="link3"> 40 </a> <li>
';

// Create a new DOM Document
$xml = new DOMDocument();

// Load the html contents into the DOM
$xml->loadHTML($html);

// Empty array to hold all links to return
$result = array();

//Loop through each <li> tag in the dom
foreach($xml->getElementsByTagName('li') as $li) {
    //Loop through each <a> tag within the li, then extract the node value
    foreach($li->getElementsByTagName('a') as $links){
    //if result equal expected number then do the logic
    }
}
//Return the links
print_r($result);
/*
Array
(
    [0] =>  36 
    [1] =>  38 
    [2] =>  40 
)

*/
?>
FreedomPride
  • 1,098
  • 1
  • 7
  • 30
0

What you want to achieve can't be done in pure php but in javascript it is possible as js is a language which is heavily oriented towards operating on DOM. Here is example thread asking for something similar to yours:

Get the text of an li element

spectatorx
  • 373
  • 2
  • 7
  • 22
  • Okay, no problem give me a javascript solution also. – Jaydip Satvara Jul 09 '18 at 10:08
  • This solution is not possible. Because in my database already have this text. There is simple li structure and which have span. I know if there is any id or unique then it will easily get but this not simple which you think. – Jaydip Satvara Jul 09 '18 at 10:12