I'd like to replace the value in this tag using PHP's str_replace function:
<address2>Replace this value</address2>
What is the best approach to do this?
Try this :
$nodeAdresseValue = str_replace("%value%", "your value", "<address2>%value%</address2>");
Do not use string functions on XML, use DOMDocument instead, it'll help you parse XML more easily, here is an example code DEMO:
<?php
$string = "<address2>Replace this value</address2>";
$domDocument = new DOMDocument();
$domDocument->loadXML($string);
$address2Elements = $domDocument->getElementsByTagName('address2');
foreach ($address2Elements as $address2) {
$address2->nodeValue = "Value Replaced";
}
var_dump($domDocument->saveXML());
Output:
string(58) "<?xml version="1.0"?>
<address2>Value Replaced</address2>
"
$search = "/[^<address2>](.*)[^<\/address2>]/";
$replace = "replace with me";
$string = "<address2>Replace this value </address2>";
echo preg_replace($search,$replace,$string);
this will work irrespective of the text, it will work even if you dont know the value inside tags
Related
I have a string that contains the following:
<img data-bind="defaultSrc: {srcDesktop: 'http://desktoplink', srcMobile: 'http://mobilelink', fallback: 'http://baseurl'}" >
I am trying to extract the srcDesktop contained inside the string. I want my final result to yield me with the link http://desktoplink. What is the best way to achieve that other than str_replace? I have a dataset that contains those strings so I am looking for a formula to extract it in php.
Here is how I have been doing it, but there is got to be a more efficient way:
$string = '<img data-bind="defaultSrc: {srcDesktop: \'http://desktoplink\', srcMobile: \'http://mobilelink\', fallback: \'http://baseurl\'}" >';
$test = explode(" ",$string);
echo "<br>".str_replace(",","",str_replace("'","",$test['3']));
You can use preg_match
$string = '<img data-bind="defaultSrc: {srcDesktop: \'http://desktoplink\', srcMobile: \'http://mobilelink\', fallback: \'http://baseurl\'}" >';
preg_match('/.*\bsrcDesktop:\s*(?:\'|\")(.*?)(?:\'|\").*/i', $string, $matches);
if (isset($matches[1])) {
echo trim($matches[1]);
}
you can use DOMDocument and json_decode to get this value, if you can change the code to the code below (added some '-signs):
$string = "<img data-bind=\"'defaultSrc': {'srcDesktop': 'http://desktoplink', 'srcMobile': 'http://mobilelink', 'fallback': 'http://baseurl'}\" >";
$doc = new DOMDocument();
$doc->loadHTML($string);
$data = str_replace('\'','"',$doc->getElementsByTagName('img')[0]->getAttribute('data-bind'));
$json = json_decode('{'.$data.'}');
var_dump($json->defaultSrc->srcDesktop);
I have a String which looks something like this:
$html_string = "<p>Some content</p><p>separated by</p><p>paragraphs</p>"
I'd like to do some parsing on the content inside the tags, so I think that creating an array from this would be easiest. Currently I'm using a series of explode and implode to achieve what I want:
$stripped = explode('<p>', $html_string);
$joined = implode(' ', $stripped);
$parsed = explode('</p>', $joined);
which in effect gives:
array('Some content', 'separated by', 'paragraphs');
Is there a better, more robust way to create an array from HTML tags? Looking at the docs, I didn't see any mention of parsing via a regular expression.
Thanks for your help!
If its only that simple with no/not much other tags inside the content you can simply use regex for that:
$string = '<p>Some content</p><p>separated by</p><p>paragraphs</p>';
preg_match_all('/<p>([^<]*?)<\/p>/mi', $string, $matches);
var_dump($matches[1]);
which creates this output:
array(3) {
[0]=>
string(12) "Some content"
[1]=>
string(12) "separated by"
[2]=>
string(10) "paragraphs"
}
Keep in mind that this is not the most effective way nor is it the fastest, but its shorter then using DOMDocument or anything like that.
If you need to do some html parsing in php, there is a nice library for that, called php html parser.
https://github.com/paquettg/php-html-parser
which can give you a jquery like api, to parse html.
an example:
// Assuming you installed from Composer:
require "vendor/autoload.php";
use PHPHtmlParser\Dom;
$dom = new Dom;
$dom->load('<p>Some content</p><p>separated by</p><p>paragraphs</p>');
$pTags = $dom->find('p');
foreach ($pTags as $tag)
{
// do something with the html
$content = $tag->innerHtml;
}
Here is the DOMDocument solution (native PHP), which will also work when your p tags have attributes, or contain other tags like <br>, or have lots of white-space in between them (which is irrelevant in HTML rendering), or contain HTML entities like or <, etc, etc:
$html_string = "<p>Some content</p><p>separated by</p><p>paragraphs</p>";
$doc = new DOMDocument();
$doc->loadHTML($html_string);
foreach($doc->getElementsByTagName('p') as $p ) {
$paras[] = $p->textContent;
}
// Output array:
print_r($paras);
If you really want to stick with regular expressions, then at least allow tag attributes and HTML entities, translating the latter to their corresponding characters:
$html_string = "<p>Some content & text</p><p>separated by</p><p style='background:yellow'>paragraphs</p>";
preg_match_all('/<p(?:\s.*?)?>\s*(.*?)\s*<\/p\s*>/si', $html_string, $matches);
$paras = $matches[1];
array_walk($paras, 'html_entity_decode');
print_r($paras);
i use (str_replace) function to replace ##ID## in youtube url with this regular expression : (?P<id>[a-z-A-Z_0-9]+)
so i use this code to do this :
<?php
$urlbase = 'https://www.youtube.com/watch?v=##ID##';
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
echo $lastchange;
?>
i get the output in the browser like this : https://www.youtube.com/watch?v=(?P[a-z-A-Z_0-9]+), its looks like <id> not show up !
i try this simple code :
<?php
echo "This is my <id>";
?>
but i just get this is my in the browser !
What's the probleme ? and how i can fix it , thanks
is being interpreted as HTML so your browser is parsing it and since it is not a renderable element, it shows nothing. Try:
<?php
echo "This is my <id>
?>
As for the str_replace, it's doing exactly what the function is supposed to be doing. If you're looking to use regular expressions in string replacements, use preg_replace
The tag <id> is being removed by your browser. It is really there if you watch the source code. Maybe you should try:
$urlbase = 'https://www.youtube.com/watch?v=##ID##';
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
echo urlencode( $lastchange );
Problem is with the line:
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
str_replace does not use regex.
You will need preg_replace
$pattern = '(<id>[a-z-A-Z_0-9]+)'
$replacement = '##ID##'
$string = $urlbase
$lastchange = preg_replace($pattern, $replacement, $string);
Also < and > are html entities which means they are reserved chars for HTML they have some special meanings if you want to show them then you must use there entity name eg < and > in your case respectively.
<?php
echo " echo "This is my <id>";
?>
I have the following that I need removed from string in loop.
<comment>Some comment here</comment>
The result is from a database so the the content inside the comment tag is different.
Thanks for the help.
Figured it out. The following seems to do the trick.
echo preg_replace('~\<comment>.*?\</comment>~', '', $blog->comment);
This may be overkill, but you can use DOMDocument to parse the string as HTML, then remove the tags.
$str = 'Test 123 <comment>Some comment here</comment> abc 456';
$dom = new DOMDocument;
// Wrap $str in a div, so we can easily extract the HTML from the DOMDocument
#$dom->loadHTML("<div id='string'>$str</div>"); // It yells about <comment> not being valid
$comments = $dom->getElementsByTagName('comment');
foreach($comments as $c){
$c->parentNode->removeChild($c);
}
$domXPath = new DOMXPath($dom);
// $dom->getElementById requires the HTML be valid, and it's not here
// $dom->saveHTML() adds a DOCTYPE and HTML tag, which we don't need
echo $domXPath->query('//div[#id="string"]')->item(0)->nodeValue; // "Test 123 abc 456"
DEMO: http://codepad.org/wfzsmpAW
If this is only a matter of removing the <comment /> tag, a simple preg_replace() or a str_replace() will do:
$input = "<comment>Some comment here</comment>";
// Probably the best method str_replace()
echo str_replace(array("<comment>","</comment>"), "", $input);
// some comment here
// Or by regular expression...
echo preg_replace("/<\/?comment>/", "", $input);
// some comment here
Or if there are other tags in there and you want to strip out all but a few, use strip_tags() with its optional second parameter to specify allowable tags.
echo strip_tags($input, "<a><p><other_allowed_tag>");
I have a string:
$string = 'some text <img src="www">';
I want to get the image source and the text.
Here is what I have:
$doc= new DOMDocument();
$doc->loadHTML($string);
$nodes=$doc->getElementsByTagName ('img');
From $nodes->item(0) I get the image source.
How can I get the the "some text"?
textContent, or with DOMXPaths $xpath->query('//text()')
For simple cases like this, try:
$doc->documentElement->textContent
You could make it like jQuery in javascript. Wrap the whole string with anything, and get this. Then you can get the TextNode, which contains this text.
$string = 'some text <img src="www">';
$string = '<div id="wrapper">' . $string . '</div>';
$nodes = $doc->getElementById('wrapper');