This question already has answers here:
PHP str_replace any number pattern
(3 answers)
Closed 1 year ago.
This question is NOT a duplicate and should not be marked as such!
I have done my research and have not found a solution. Standard replacements are not a solution here (How do I replace certain parts of my string? is not helpful). It is very annoying that every question asked on Stack Overflow is marked as duplicate and closed directly after less than 5 minutes. Obviously without even having read/understood the question.
Back to the problem:
Here's an example:
<?php
$string = '<span class="X">001</span>';
$string .= '<span class="X">444</span>';
$string .= '<span class="X">242</span>';
$string .= '<span class="X">334</span>';
?>
Now I want to replace every occurrence from <span class="X"> to <span> (including the ! unknown ! number contained) with another string, for example <div title="yay">Icecream</div>.
So that the result could be:
<?php
echo $string;
// outputs:
// <div title="yay">Icecream</div>
// <div title="yay">Icecream</div>
// <div title="yay">Icecream</div>
// <div title="yay">Icecream</div>
?>
How can this be achieved?
For anyone facing the same problem, here's a possible answer:
<?php
$string = '<span class="X">001</span>';
$string .= '<span class="X">444</span>';
$string .= '<span class="X">242</span>';
$string .= '<span class="X">334</span>';
$replace = preg_replace('#<span class="X">.*</span>#m', '<div title="yay">Icecream</div>', $string);
echo $replace;
?>
Related
This question already has answers here:
Regex select all text between tags
(23 answers)
Closed 2 years ago.
I'm trying to write a function, which will find each substring in string, where substring is some html tag, for example
<li>
But my regular expression don't work and i can't finde my mistake.
$str = 'hello brbrbr <li> hello</li> <li>how are you?</li>';
$items = preg_match_all('/(<li>\w+<\/li>)', $str, $matches);
$items must be an array of the desired substrings
Consider using DOMDocument to parse and manipulate HTML or XML tags. Do not reinvent the wheel with Regex.
$str = 'hello brbrbr <li> hello</li> <li>how are you?</li>';
$dom = new DOMDocument();
$dom->loadHTML($str);
$li = $dom->getElementsByTagName('li');
$value = $li->item(0)->nodeValue;
echo $value;
' hello'
Or if you want to iterate over all
foreach($li as $item)
echo $item->nodeValue, PHP_EOL;
' hello'
'how are you?'
Markus' answer is correct but in case you just want the fast and dirty regex one, here it is:
$str = 'hello brbrbr <li> hello</li> <li>how are you?</li>';
preg_match_all('/(<li>.+<\/li>)/U', $str, $items);
U makes it ungreedy.
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
The following is automatically output via editor:
1001-web-file
I would like to add an ID:
<a id="replace" href="https://someurl.com/1001-web-file.pdf">1001-web-file</a>
Then use str_replace and a regular expression to find the anchors in question and replace with:
<img src="https://someurl.com/1001-web-file-pdf-290x300.jpg"/>
What I've managed to do is:
$replace = array(
'<a id="pdfthumb" href="' => '<img src="',
'.pdf">pdfthumb</a>' => '-pdf-290x300.jpg"/></br>'
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
This works to tear down the anchor tag and rebuild as an img. But I can't do much more. I played around with some regex to create a wildcard and realized I need to create a variable for the href to use when I rebuild the HTML, but I'm stuck.
Any insight is much appreciated :)
In your case, I think it should be easier if you do it on client side using javascript.
My background is not PHP but you can use the same pattern to test with your PHP code:
Input:
1001-web-file
Output:
<img src="https://someurl.com/1001-web-file-pdf-290x300.jpg"/>
var input = '1001-web-file';
var pattern = /href=\"(.+)\.pdf\"/;
var match = input.match(pattern)[1];
input = input.replace(/(<a.*>).*(<\/a>)/, '$1<img src="' + match + '-pdf-290x300.jpg">$2');
console.log(input)
Here is translated php code of Tan javascript answer
$input = '1001-web-file';
preg_match('/href="(.+)\.pdf"/', $input, $m);
$match = $m[1];
$input = preg_replace('/(<a.*>).*(<\/a>)/', '$1<img src="'. $match .'-pdf-290x300.jpg">$2', $input);
echo $input;
This question already has answers here:
Remove style attribute from HTML tags
(9 answers)
Closed 9 years ago.
In my code I get a string which have html tags like so:
$string = '<div style="width:100px;">ABC 1234 <span> Test string, testing this string</span></div>';
Now, I removed the style attribute from the said string using preg_replace:
$string = preg_replace('/(<[^>]+) style=".*?"/i', '', $string);
After removing the style tag, I managed to remove the style attribute so the div tag ended up looking like <div>. The problem, I encountered after doing this is that I now get an excess > after the closing tag for the span so the string looks like this now:
$string = '<div>ABC 1234 <span> Test string, testing this string</span> > </div>';
My question is, why did I suddenly get an exccess >? Is there a different regular expression I can use that will get rid of the style attribute without the additional > appearing? Or is there any way I can get ride of this?
I tried using str_replace twice like so:
$string = str_replace("\n", "", $string);
$string = str_replace(">>", ">", $string);
But that did not work either.
I am not trying to remove the HTML tags, just the style part.
Use it only for this string.
<?php
$string = "<div style=\"width:100px;\">ABC 1234 <span> Test string, testing this string</span></div>";
$string = strip_tags($string,"<span>");
$string = "<div>".$string."</div>";
?>
Now the string is:
<div>ABC 1234 <span> Test string, testing this string</span></div>
I used This
$string = '<div style="width:100px;">ABC 1234 <span> Test string, testing this string</span></div>';
$output = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $string);
die(htmlentities($output))
and the output is
<div>ABC 1234 <span> Test string, testing this string</span></div>
as you need
This question already has answers here:
Explode a paragraph into sentences in PHP
(8 answers)
Closed 9 years ago.
I would like to add span tag for every sentence which ended with "."
like:
my string:
"I could not have said, ’Betrayest thou the Son of man with a kiss?’ unless I believed in betrayal. The whole message of the crucifixion was simply that I did not."
O/P:
"<span id="s1">I could not have said, ’Betrayest thou the Son of man with a kiss?’ unless I believed in betrayal.</span> <span id="s2">The whole message of the crucifixion was simply that I did not.</span>"
how it possible with php?
You could do
<?php
$string="I could not have said, ’Betrayest thou the Son of man with a kiss?’ unless I believed in betrayal. The whole message of the crucifixion was simply that I did not.";
$output=str_replace(". ",'. </span> <span id="s2">',$string);
echo '<span id="s1">'.$output.'</span>';
?>
Edit Based on Comments
This version will make sure every new replacement gets a new span id
<?php
$string="I could not have said, ’Betrayest thou the Son of man with a kiss?’ unless I believed in betrayal. The whole message of the crucifixion was simply that I did not. Testing 123. testing again.";
$dots=substr_count($string,". ");
for($i=2;$i<=$dots+2;$i++)
{
$string=preg_replace("/\. /", ".</span> <span id =\"s$i\">" ,$string,1);
}
echo '<span id="s1">'.$string.'</span>';
?>
If you explode the sentence with ". ". I want to modify the code above.
$newText = "";
$count = 0;
foreach (explode(". ",$theText) as $part) {
if(ctype_upper($part{0}))
{
$count++;
$newText .= "<span id=\"s$count\">$part</span>";
}
}
I hope it should work for abbreviations or something.
Try this code,
You will get unique id for span tag.
<?php
$str = "I could not have said, 'Betrayest thou the Son of man with a kiss?' unless I believed in betrayal. The whole message of the crucifixion was simply that I did not.";
$exp_str = explode(". ",$str);
$sentence = "<span id=\"s1\">";
$n = 2;
foreach($exp_str as $val) {
$sentence .= $val.".</span> <span id=\"s$n\">";
$n++;
}
$n = $n-1;
$sentence = substr($sentence,0,strpos($sentence,".</span> <span id=\"s$n\">"));
echo $sentence;
?>
I'm not sure if preg_replace can do this (because of the unique IDs). Otherwise it can be manually done like this:
$newText = "";
$count = 0;
foreach (explode(".",$theText) as $part) {
$count++;
$newText .= "<span id=\"s$count\">$part</span>";
}
But what about periods mid sentence, as in abbreviations or something? You could probably replace the explode with a preg_split to get a better result. For instance only split if the char right after the period is not a letter or another period.
preg_split("/\.[^\,\w]/",$theText);
Or just ensure the next char is a whitespace.
preg_split("/\.\s/",$theText);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php regexp: remove all attributes from an html tag
$input = '<div style="font-size: 18px; line-height: 20px;" id="whatever">text</div>';
// ... some code here, probably regex
$desired_output = '<div id="whatever">text</div>';
How do I do the above using PHP?
try the following you need a regular expression to strip it out.
$desired_output = preg_replace('/<\s*style.+?<\s*\/\s*style.*?>/si', ' ', $input );
or this
$desired_output = preg_replace('%style="[^"]+"%i', '', $input);