Extract text between 2 strings? [duplicate] - php

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 7 years ago.
I have a huge HTML page that contains multiple data like this
<td style="font-size:24px;" bgcolor="#F0F0F0" width="60%">
<strong>ID:Full Name:email#email.com:Mobile:Country</strong>
</td>
I want to extract the data between the tags which is ID:Full Name:email#email.com:Mobile:Country
So what would be the regex or any custom PHP Function?
PS: The above code is being repeated multiple times in a page and I want all that data to be stored in an array.

As of others has said, you can just use DOMDocument and DOMXpath. Like this:
$html = '<td style="font-size:24px;" bgcolor="#F0F0F0" width="60%"> <strong>ID:Full Name:email#email.com:Mobile:Country</strong></td>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
$text = $xpath->query('//td/strong')->item(0)->nodeValue;
echo $text; // ID:Full Name:email#email.com:Mobile:Country

Related

php replace id element content in string [duplicate]

This question already has answers here:
PHP DOMDocument replace content by div id
(1 answer)
How do you parse and process HTML/XML in PHP?
(31 answers)
Find an element by id and replace its contents with php
(3 answers)
Change innerHTML of a php DOMElement [duplicate]
(10 answers)
DOM change element content
(1 answer)
Closed 3 years ago.
a bit stuck, is it possible to use PHP to update/change html element contents.
So replace only 'Hello World' in the string below based off the the id name.
$html = '<h1 id="item" class="abc" data="efg">Hello World</h1>';
Perhaps using preg_replace, just want to leave all other content in there, eg. class, data, etc.
$html = preg_replace('<h1 id="item">????</h1>', 'New Content', $html);
Thanks
May be below code will help
$html = '<h1 id="item" class="abc" data="efg">Hello World</h1>';
$elementId = "item";
$newString = "Replace World";
$dom = new DOMDocument();
$dom->loadHTML($html);
$belement = $dom->getElementById("$elementId");
$oldString = $belement->nodeValue;
$newHTML = str_replace("$oldString","$newString","$html");
echo $newHTML;
Note: Please change $elementId and $newString with original values

dom x path to grab href value [duplicate]

This question already has answers here:
How to extract a node attribute from XML using PHP's DOM Parser
(3 answers)
Closed 3 years ago.
I have the following html
<div class="logo">***® text.<sup>TM</sup></div>
I would like to get the value of href with php dom xpath, how would I accomplish that?
This is what I have tried:
$anchors = $domXpath->query("//div[#class='logo']/a");
foreach($anchors as $a)
{
print $a->nodeValue." - ".$a->getAttribute("href")."<br/>";
}
Here is the solution.
$xpath = new DOMXpath($dom);
$link = $xpath->query('//div[#class="logo"]/a');
$link->getAttribute('href')

String between php [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 6 years ago.
I've got something like this:
$string = '<some code before><div class="abc">Something written here</div><some other code after>'
What I want is to get what is within the div and output it:
Something written here
How can I do that in php? Thanks in advance!
You would use the DOMDocument class.
// HTML document stored in a string
$html = '<strong><div class="abc">Something written here</div></strong>';
// Load the HTML document
$dom = new DOMDocument();
$dom->loadHTML($html);
// Find div with class 'abc'
$xpath = new DOMXPath($dom);
$result = $xpath->query('//div[#class="abc"]');
// Echo the results...
if($result->length > 0) {
foreach($result as $node) {
echo $node->nodeValue,"\n";
}
} else {
echo "Empty result set\n";
}
Read up on the expression syntax for XPath to customize your DOM searches.

XPATH - preserve spaces with getAttribute [duplicate]

This question already has answers here:
XPath get attribute value in PHP [duplicate]
(3 answers)
Closed 8 years ago.
HTML
<input type='text' name='title[]' value='Some word and another'>
PHP
$title = $xpath->query('//input')->item(0);
echo $title = $title->getAttribute('value')
RESULT
Some
I NEED TO GET
Some word and another
PROBLEM
For some reason everything after first space is stripped out..
Be sure to load it as HTML, so that it allows you to use single quotes:
$dom = new DOMDocument;
$dom->loadHTML("<input type='text' name='title[]' value='Some word and another'>");
$xpath = new DOMXpath($dom);
echo $xpath->query('//input')->item(0)->getAttribute('value');
See it here in action: http://codepad.viper-7.com/pcs3or

php DOMDocument How to convert node value to string [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can I get an element's serialised HTML with PHP's DOMDocument?
PHP + DOMDocument: outerHTML for element?
I am trying to extract all img tags from a string. I am using:
$domimg = new DOMDocument();
#$domimg->loadHTML($body);
$images_all = $domimg->getElementsByTagName('img');
foreach ($images_all as $image) {
// do something
}
I want to put the src= values or even the complete img tags into an array or string.
Use saveXML() or saveHTML() on each node to add it to an array:
$img_links = array();
$domimg = new DOMDocument();
$domimg->loadHTML($body);
$images_all = $domimg->getElementsByTagName('img');
foreach ($images_all as $image) {
// Append the XML or HTML of each to an array
$img_links[] = $domimg->saveXML($image);
}
print_r($img_links);
You could try a DOM parser like simplexml_load_string. Take a look at a similar answer I posted here:
Needle in haystack with array in PHP

Categories