PHP str_replace() and preg_replace() not working with HTML





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















When I try to do str_replace() or preg_replace() within the function, the content does not change.



Content in variable $sadrzaj:



$sadrzaj = '<p>asdasdasds</p><p><a href="http://www.example.com/wp-content/uploads/2018/11/image.jpg" itemprop="url" title="some title"><img alt="some alt title" class="alignnone size-full wp-image-243618" src="http://www.example.com/wp-content/uploads/2018/11/image.jpg" width="940" height="529"></a></p>asdasdasd<p>asdasd</p><h3>asdada</h3><p><a href="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" itemprop="url" title="some title 02"><img alt="some alt title 02" class="alignnone size-full wp-image-243653" src="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" width="940" height="529"></a></p><h3>asdasd</h3>';


My function to_je_to():



function to_je_to($content){
preg_match_all('/<img (.*?)/>/', $content, $images);
//print_r($images);

if(!is_null($images)){
foreach($images[1] as $index => $value){
if(strpos($images[1], 'size-full') !== false){
//if(preg_match('/alt=""/', $value)){
$new_img = preg_replace('<img', "<img data-example", $images[0][$index]);
$content = preg_replace($images[0][$index], $new_img, $content);
}
}
}
echo $content; // return no difference
}


Calling the function to_je_to($sadrzaj); - nothing changes.



If there is class with "size-full", find this images and replace their tag with <img data-example ...>.



Even str_replace() or preg_replace() is not working.



What am I doing wrong?



Thanks










share|improve this question




















  • 1





    Your img tags do not end with /> but only >, so your regex doesn't capture them.

    – Jeto
    Nov 26 '18 at 23:49








  • 1





    ^^^ Here's why we don't parse HTML with regular expressions...

    – miken32
    Nov 26 '18 at 23:55






  • 1





    @miken32 That's right. A DOM parser would seem far more appropriate.

    – Jeto
    Nov 26 '18 at 23:57











  • Thanks for feedforward for my drawbacks. I will keep in mind for further programming.

    – Kiki FIstrek Novi
    Nov 27 '18 at 0:00


















-1















When I try to do str_replace() or preg_replace() within the function, the content does not change.



Content in variable $sadrzaj:



$sadrzaj = '<p>asdasdasds</p><p><a href="http://www.example.com/wp-content/uploads/2018/11/image.jpg" itemprop="url" title="some title"><img alt="some alt title" class="alignnone size-full wp-image-243618" src="http://www.example.com/wp-content/uploads/2018/11/image.jpg" width="940" height="529"></a></p>asdasdasd<p>asdasd</p><h3>asdada</h3><p><a href="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" itemprop="url" title="some title 02"><img alt="some alt title 02" class="alignnone size-full wp-image-243653" src="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" width="940" height="529"></a></p><h3>asdasd</h3>';


My function to_je_to():



function to_je_to($content){
preg_match_all('/<img (.*?)/>/', $content, $images);
//print_r($images);

if(!is_null($images)){
foreach($images[1] as $index => $value){
if(strpos($images[1], 'size-full') !== false){
//if(preg_match('/alt=""/', $value)){
$new_img = preg_replace('<img', "<img data-example", $images[0][$index]);
$content = preg_replace($images[0][$index], $new_img, $content);
}
}
}
echo $content; // return no difference
}


Calling the function to_je_to($sadrzaj); - nothing changes.



If there is class with "size-full", find this images and replace their tag with <img data-example ...>.



Even str_replace() or preg_replace() is not working.



What am I doing wrong?



Thanks










share|improve this question




















  • 1





    Your img tags do not end with /> but only >, so your regex doesn't capture them.

    – Jeto
    Nov 26 '18 at 23:49








  • 1





    ^^^ Here's why we don't parse HTML with regular expressions...

    – miken32
    Nov 26 '18 at 23:55






  • 1





    @miken32 That's right. A DOM parser would seem far more appropriate.

    – Jeto
    Nov 26 '18 at 23:57











  • Thanks for feedforward for my drawbacks. I will keep in mind for further programming.

    – Kiki FIstrek Novi
    Nov 27 '18 at 0:00














-1












-1








-1








When I try to do str_replace() or preg_replace() within the function, the content does not change.



Content in variable $sadrzaj:



$sadrzaj = '<p>asdasdasds</p><p><a href="http://www.example.com/wp-content/uploads/2018/11/image.jpg" itemprop="url" title="some title"><img alt="some alt title" class="alignnone size-full wp-image-243618" src="http://www.example.com/wp-content/uploads/2018/11/image.jpg" width="940" height="529"></a></p>asdasdasd<p>asdasd</p><h3>asdada</h3><p><a href="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" itemprop="url" title="some title 02"><img alt="some alt title 02" class="alignnone size-full wp-image-243653" src="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" width="940" height="529"></a></p><h3>asdasd</h3>';


My function to_je_to():



function to_je_to($content){
preg_match_all('/<img (.*?)/>/', $content, $images);
//print_r($images);

if(!is_null($images)){
foreach($images[1] as $index => $value){
if(strpos($images[1], 'size-full') !== false){
//if(preg_match('/alt=""/', $value)){
$new_img = preg_replace('<img', "<img data-example", $images[0][$index]);
$content = preg_replace($images[0][$index], $new_img, $content);
}
}
}
echo $content; // return no difference
}


Calling the function to_je_to($sadrzaj); - nothing changes.



If there is class with "size-full", find this images and replace their tag with <img data-example ...>.



Even str_replace() or preg_replace() is not working.



What am I doing wrong?



Thanks










share|improve this question
















When I try to do str_replace() or preg_replace() within the function, the content does not change.



Content in variable $sadrzaj:



$sadrzaj = '<p>asdasdasds</p><p><a href="http://www.example.com/wp-content/uploads/2018/11/image.jpg" itemprop="url" title="some title"><img alt="some alt title" class="alignnone size-full wp-image-243618" src="http://www.example.com/wp-content/uploads/2018/11/image.jpg" width="940" height="529"></a></p>asdasdasd<p>asdasd</p><h3>asdada</h3><p><a href="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" itemprop="url" title="some title 02"><img alt="some alt title 02" class="alignnone size-full wp-image-243653" src="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" width="940" height="529"></a></p><h3>asdasd</h3>';


My function to_je_to():



function to_je_to($content){
preg_match_all('/<img (.*?)/>/', $content, $images);
//print_r($images);

if(!is_null($images)){
foreach($images[1] as $index => $value){
if(strpos($images[1], 'size-full') !== false){
//if(preg_match('/alt=""/', $value)){
$new_img = preg_replace('<img', "<img data-example", $images[0][$index]);
$content = preg_replace($images[0][$index], $new_img, $content);
}
}
}
echo $content; // return no difference
}


Calling the function to_je_to($sadrzaj); - nothing changes.



If there is class with "size-full", find this images and replace their tag with <img data-example ...>.



Even str_replace() or preg_replace() is not working.



What am I doing wrong?



Thanks







php regex preg-replace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 0:50









miken32

24.8k95173




24.8k95173










asked Nov 26 '18 at 23:28









Kiki FIstrek NoviKiki FIstrek Novi

267




267








  • 1





    Your img tags do not end with /> but only >, so your regex doesn't capture them.

    – Jeto
    Nov 26 '18 at 23:49








  • 1





    ^^^ Here's why we don't parse HTML with regular expressions...

    – miken32
    Nov 26 '18 at 23:55






  • 1





    @miken32 That's right. A DOM parser would seem far more appropriate.

    – Jeto
    Nov 26 '18 at 23:57











  • Thanks for feedforward for my drawbacks. I will keep in mind for further programming.

    – Kiki FIstrek Novi
    Nov 27 '18 at 0:00














  • 1





    Your img tags do not end with /> but only >, so your regex doesn't capture them.

    – Jeto
    Nov 26 '18 at 23:49








  • 1





    ^^^ Here's why we don't parse HTML with regular expressions...

    – miken32
    Nov 26 '18 at 23:55






  • 1





    @miken32 That's right. A DOM parser would seem far more appropriate.

    – Jeto
    Nov 26 '18 at 23:57











  • Thanks for feedforward for my drawbacks. I will keep in mind for further programming.

    – Kiki FIstrek Novi
    Nov 27 '18 at 0:00








1




1





Your img tags do not end with /> but only >, so your regex doesn't capture them.

– Jeto
Nov 26 '18 at 23:49







Your img tags do not end with /> but only >, so your regex doesn't capture them.

– Jeto
Nov 26 '18 at 23:49






1




1





^^^ Here's why we don't parse HTML with regular expressions...

– miken32
Nov 26 '18 at 23:55





^^^ Here's why we don't parse HTML with regular expressions...

– miken32
Nov 26 '18 at 23:55




1




1





@miken32 That's right. A DOM parser would seem far more appropriate.

– Jeto
Nov 26 '18 at 23:57





@miken32 That's right. A DOM parser would seem far more appropriate.

– Jeto
Nov 26 '18 at 23:57













Thanks for feedforward for my drawbacks. I will keep in mind for further programming.

– Kiki FIstrek Novi
Nov 27 '18 at 0:00





Thanks for feedforward for my drawbacks. I will keep in mind for further programming.

– Kiki FIstrek Novi
Nov 27 '18 at 0:00












2 Answers
2






active

oldest

votes


















3














What you're doing wrong is parsing HTML with a regular expression. You should use a proper DOM parser and then you can use XPath queries to isolate your desired elements.



<?php
$sadrzaj = '<p>asdasdasds</p><p><a href="http://www.example.com/wp-content/uploads/2018/11/image.jpg" itemprop="url" title="some title"><img alt="some alt title" class="alignnone size-full wp-image-243618" src="http://www.example.com/wp-content/uploads/2018/11/image.jpg" width="940" height="529"></a></p>asdasdasd<p>asdasd</p><h3>asdada</h3><p><a href="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" itemprop="url" title="some title 02"><img alt="some alt title 02" class="alignnone size-full wp-image-243653" src="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" width="940" height="529"></a></p><h3>asdasd</h3>';

function to_je_to($content) {
$dom = new DomDocument;
$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xp = new DomXpath($dom);
$nodes = $xp->query("//img[contains(concat(' ', normalize-space(@class), ' '), ' size-full ')]");
foreach ($nodes as $img) {
$img->setAttribute("data-example", "");
}
return $dom->saveHTML();
}
echo to_je_to($sadrzaj);




And, commenting on your original code: $images will never be null, it will always be an array. Why loop over $images[1] and then replace values based on $images[0]? There's no use of the group match (.*?) at all so it doesn't need to be there. Neither of the preg_replace() calls in the loop use delimiters around the expression to search, so both would have failed with errors. And there is a very big difference between echo and return.






share|improve this answer


























  • @KikiFIstrekNovi this is a better answer than mine. You should accept it instead.

    – Nick
    Nov 27 '18 at 2:15



















2














There are a few issues with your function (in your preg_match_all, and preg_replace). Also, you need to be more sophisticated in terms of matching the class on the <img> tag. Overall, you would be better off using the built-in DOMDocument class as the other answer proposes. If you want to keep using regex, this function should fix the issues you had.



function to_je_to($content){
preg_match_all('/<img[^>]+>/', $content, $images);

if(!is_null($images)){
foreach($images[0] as $index => $value){
if(preg_match('/class="[^"]*(?<=["s])size-full[s"]/', $value)){
$new_img = str_replace('<img', '<img data-example', $value);
$content = preg_replace('/' . preg_quote($value, '/') . '/', $new_img, $content);
}
}
}
return $content; // return no difference
}
echo to_je_to($sadrzaj);


Demo on 3v4l.org






share|improve this answer


























  • Awesome. I really appreciate your help in resolving the problem and your time. Will keep in mind this

    – Kiki FIstrek Novi
    Nov 27 '18 at 0:01











  • There are errors with this function, I'm seeing double < in the output. And I won't go on too much about the many ways this could fail (e.g. class='not-size-full' or alt="this is one size-full too many")

    – miken32
    Nov 27 '18 at 0:13













  • @KikiFIstrekNovi please see my edited answer. I have addressed all the valid points that miken32 has raised.

    – Nick
    Nov 27 '18 at 2:11












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490631%2fphp-str-replace-and-preg-replace-not-working-with-html%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














What you're doing wrong is parsing HTML with a regular expression. You should use a proper DOM parser and then you can use XPath queries to isolate your desired elements.



<?php
$sadrzaj = '<p>asdasdasds</p><p><a href="http://www.example.com/wp-content/uploads/2018/11/image.jpg" itemprop="url" title="some title"><img alt="some alt title" class="alignnone size-full wp-image-243618" src="http://www.example.com/wp-content/uploads/2018/11/image.jpg" width="940" height="529"></a></p>asdasdasd<p>asdasd</p><h3>asdada</h3><p><a href="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" itemprop="url" title="some title 02"><img alt="some alt title 02" class="alignnone size-full wp-image-243653" src="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" width="940" height="529"></a></p><h3>asdasd</h3>';

function to_je_to($content) {
$dom = new DomDocument;
$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xp = new DomXpath($dom);
$nodes = $xp->query("//img[contains(concat(' ', normalize-space(@class), ' '), ' size-full ')]");
foreach ($nodes as $img) {
$img->setAttribute("data-example", "");
}
return $dom->saveHTML();
}
echo to_je_to($sadrzaj);




And, commenting on your original code: $images will never be null, it will always be an array. Why loop over $images[1] and then replace values based on $images[0]? There's no use of the group match (.*?) at all so it doesn't need to be there. Neither of the preg_replace() calls in the loop use delimiters around the expression to search, so both would have failed with errors. And there is a very big difference between echo and return.






share|improve this answer


























  • @KikiFIstrekNovi this is a better answer than mine. You should accept it instead.

    – Nick
    Nov 27 '18 at 2:15
















3














What you're doing wrong is parsing HTML with a regular expression. You should use a proper DOM parser and then you can use XPath queries to isolate your desired elements.



<?php
$sadrzaj = '<p>asdasdasds</p><p><a href="http://www.example.com/wp-content/uploads/2018/11/image.jpg" itemprop="url" title="some title"><img alt="some alt title" class="alignnone size-full wp-image-243618" src="http://www.example.com/wp-content/uploads/2018/11/image.jpg" width="940" height="529"></a></p>asdasdasd<p>asdasd</p><h3>asdada</h3><p><a href="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" itemprop="url" title="some title 02"><img alt="some alt title 02" class="alignnone size-full wp-image-243653" src="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" width="940" height="529"></a></p><h3>asdasd</h3>';

function to_je_to($content) {
$dom = new DomDocument;
$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xp = new DomXpath($dom);
$nodes = $xp->query("//img[contains(concat(' ', normalize-space(@class), ' '), ' size-full ')]");
foreach ($nodes as $img) {
$img->setAttribute("data-example", "");
}
return $dom->saveHTML();
}
echo to_je_to($sadrzaj);




And, commenting on your original code: $images will never be null, it will always be an array. Why loop over $images[1] and then replace values based on $images[0]? There's no use of the group match (.*?) at all so it doesn't need to be there. Neither of the preg_replace() calls in the loop use delimiters around the expression to search, so both would have failed with errors. And there is a very big difference between echo and return.






share|improve this answer


























  • @KikiFIstrekNovi this is a better answer than mine. You should accept it instead.

    – Nick
    Nov 27 '18 at 2:15














3












3








3







What you're doing wrong is parsing HTML with a regular expression. You should use a proper DOM parser and then you can use XPath queries to isolate your desired elements.



<?php
$sadrzaj = '<p>asdasdasds</p><p><a href="http://www.example.com/wp-content/uploads/2018/11/image.jpg" itemprop="url" title="some title"><img alt="some alt title" class="alignnone size-full wp-image-243618" src="http://www.example.com/wp-content/uploads/2018/11/image.jpg" width="940" height="529"></a></p>asdasdasd<p>asdasd</p><h3>asdada</h3><p><a href="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" itemprop="url" title="some title 02"><img alt="some alt title 02" class="alignnone size-full wp-image-243653" src="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" width="940" height="529"></a></p><h3>asdasd</h3>';

function to_je_to($content) {
$dom = new DomDocument;
$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xp = new DomXpath($dom);
$nodes = $xp->query("//img[contains(concat(' ', normalize-space(@class), ' '), ' size-full ')]");
foreach ($nodes as $img) {
$img->setAttribute("data-example", "");
}
return $dom->saveHTML();
}
echo to_je_to($sadrzaj);




And, commenting on your original code: $images will never be null, it will always be an array. Why loop over $images[1] and then replace values based on $images[0]? There's no use of the group match (.*?) at all so it doesn't need to be there. Neither of the preg_replace() calls in the loop use delimiters around the expression to search, so both would have failed with errors. And there is a very big difference between echo and return.






share|improve this answer















What you're doing wrong is parsing HTML with a regular expression. You should use a proper DOM parser and then you can use XPath queries to isolate your desired elements.



<?php
$sadrzaj = '<p>asdasdasds</p><p><a href="http://www.example.com/wp-content/uploads/2018/11/image.jpg" itemprop="url" title="some title"><img alt="some alt title" class="alignnone size-full wp-image-243618" src="http://www.example.com/wp-content/uploads/2018/11/image.jpg" width="940" height="529"></a></p>asdasdasd<p>asdasd</p><h3>asdada</h3><p><a href="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" itemprop="url" title="some title 02"><img alt="some alt title 02" class="alignnone size-full wp-image-243653" src="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" width="940" height="529"></a></p><h3>asdasd</h3>';

function to_je_to($content) {
$dom = new DomDocument;
$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xp = new DomXpath($dom);
$nodes = $xp->query("//img[contains(concat(' ', normalize-space(@class), ' '), ' size-full ')]");
foreach ($nodes as $img) {
$img->setAttribute("data-example", "");
}
return $dom->saveHTML();
}
echo to_je_to($sadrzaj);




And, commenting on your original code: $images will never be null, it will always be an array. Why loop over $images[1] and then replace values based on $images[0]? There's no use of the group match (.*?) at all so it doesn't need to be there. Neither of the preg_replace() calls in the loop use delimiters around the expression to search, so both would have failed with errors. And there is a very big difference between echo and return.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '18 at 0:45

























answered Nov 27 '18 at 0:09









miken32miken32

24.8k95173




24.8k95173













  • @KikiFIstrekNovi this is a better answer than mine. You should accept it instead.

    – Nick
    Nov 27 '18 at 2:15



















  • @KikiFIstrekNovi this is a better answer than mine. You should accept it instead.

    – Nick
    Nov 27 '18 at 2:15

















@KikiFIstrekNovi this is a better answer than mine. You should accept it instead.

– Nick
Nov 27 '18 at 2:15





@KikiFIstrekNovi this is a better answer than mine. You should accept it instead.

– Nick
Nov 27 '18 at 2:15













2














There are a few issues with your function (in your preg_match_all, and preg_replace). Also, you need to be more sophisticated in terms of matching the class on the <img> tag. Overall, you would be better off using the built-in DOMDocument class as the other answer proposes. If you want to keep using regex, this function should fix the issues you had.



function to_je_to($content){
preg_match_all('/<img[^>]+>/', $content, $images);

if(!is_null($images)){
foreach($images[0] as $index => $value){
if(preg_match('/class="[^"]*(?<=["s])size-full[s"]/', $value)){
$new_img = str_replace('<img', '<img data-example', $value);
$content = preg_replace('/' . preg_quote($value, '/') . '/', $new_img, $content);
}
}
}
return $content; // return no difference
}
echo to_je_to($sadrzaj);


Demo on 3v4l.org






share|improve this answer


























  • Awesome. I really appreciate your help in resolving the problem and your time. Will keep in mind this

    – Kiki FIstrek Novi
    Nov 27 '18 at 0:01











  • There are errors with this function, I'm seeing double < in the output. And I won't go on too much about the many ways this could fail (e.g. class='not-size-full' or alt="this is one size-full too many")

    – miken32
    Nov 27 '18 at 0:13













  • @KikiFIstrekNovi please see my edited answer. I have addressed all the valid points that miken32 has raised.

    – Nick
    Nov 27 '18 at 2:11
















2














There are a few issues with your function (in your preg_match_all, and preg_replace). Also, you need to be more sophisticated in terms of matching the class on the <img> tag. Overall, you would be better off using the built-in DOMDocument class as the other answer proposes. If you want to keep using regex, this function should fix the issues you had.



function to_je_to($content){
preg_match_all('/<img[^>]+>/', $content, $images);

if(!is_null($images)){
foreach($images[0] as $index => $value){
if(preg_match('/class="[^"]*(?<=["s])size-full[s"]/', $value)){
$new_img = str_replace('<img', '<img data-example', $value);
$content = preg_replace('/' . preg_quote($value, '/') . '/', $new_img, $content);
}
}
}
return $content; // return no difference
}
echo to_je_to($sadrzaj);


Demo on 3v4l.org






share|improve this answer


























  • Awesome. I really appreciate your help in resolving the problem and your time. Will keep in mind this

    – Kiki FIstrek Novi
    Nov 27 '18 at 0:01











  • There are errors with this function, I'm seeing double < in the output. And I won't go on too much about the many ways this could fail (e.g. class='not-size-full' or alt="this is one size-full too many")

    – miken32
    Nov 27 '18 at 0:13













  • @KikiFIstrekNovi please see my edited answer. I have addressed all the valid points that miken32 has raised.

    – Nick
    Nov 27 '18 at 2:11














2












2








2







There are a few issues with your function (in your preg_match_all, and preg_replace). Also, you need to be more sophisticated in terms of matching the class on the <img> tag. Overall, you would be better off using the built-in DOMDocument class as the other answer proposes. If you want to keep using regex, this function should fix the issues you had.



function to_je_to($content){
preg_match_all('/<img[^>]+>/', $content, $images);

if(!is_null($images)){
foreach($images[0] as $index => $value){
if(preg_match('/class="[^"]*(?<=["s])size-full[s"]/', $value)){
$new_img = str_replace('<img', '<img data-example', $value);
$content = preg_replace('/' . preg_quote($value, '/') . '/', $new_img, $content);
}
}
}
return $content; // return no difference
}
echo to_je_to($sadrzaj);


Demo on 3v4l.org






share|improve this answer















There are a few issues with your function (in your preg_match_all, and preg_replace). Also, you need to be more sophisticated in terms of matching the class on the <img> tag. Overall, you would be better off using the built-in DOMDocument class as the other answer proposes. If you want to keep using regex, this function should fix the issues you had.



function to_je_to($content){
preg_match_all('/<img[^>]+>/', $content, $images);

if(!is_null($images)){
foreach($images[0] as $index => $value){
if(preg_match('/class="[^"]*(?<=["s])size-full[s"]/', $value)){
$new_img = str_replace('<img', '<img data-example', $value);
$content = preg_replace('/' . preg_quote($value, '/') . '/', $new_img, $content);
}
}
}
return $content; // return no difference
}
echo to_je_to($sadrzaj);


Demo on 3v4l.org







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '18 at 2:51

























answered Nov 26 '18 at 23:56









NickNick

39.9k132443




39.9k132443













  • Awesome. I really appreciate your help in resolving the problem and your time. Will keep in mind this

    – Kiki FIstrek Novi
    Nov 27 '18 at 0:01











  • There are errors with this function, I'm seeing double < in the output. And I won't go on too much about the many ways this could fail (e.g. class='not-size-full' or alt="this is one size-full too many")

    – miken32
    Nov 27 '18 at 0:13













  • @KikiFIstrekNovi please see my edited answer. I have addressed all the valid points that miken32 has raised.

    – Nick
    Nov 27 '18 at 2:11



















  • Awesome. I really appreciate your help in resolving the problem and your time. Will keep in mind this

    – Kiki FIstrek Novi
    Nov 27 '18 at 0:01











  • There are errors with this function, I'm seeing double < in the output. And I won't go on too much about the many ways this could fail (e.g. class='not-size-full' or alt="this is one size-full too many")

    – miken32
    Nov 27 '18 at 0:13













  • @KikiFIstrekNovi please see my edited answer. I have addressed all the valid points that miken32 has raised.

    – Nick
    Nov 27 '18 at 2:11

















Awesome. I really appreciate your help in resolving the problem and your time. Will keep in mind this

– Kiki FIstrek Novi
Nov 27 '18 at 0:01





Awesome. I really appreciate your help in resolving the problem and your time. Will keep in mind this

– Kiki FIstrek Novi
Nov 27 '18 at 0:01













There are errors with this function, I'm seeing double < in the output. And I won't go on too much about the many ways this could fail (e.g. class='not-size-full' or alt="this is one size-full too many")

– miken32
Nov 27 '18 at 0:13







There are errors with this function, I'm seeing double < in the output. And I won't go on too much about the many ways this could fail (e.g. class='not-size-full' or alt="this is one size-full too many")

– miken32
Nov 27 '18 at 0:13















@KikiFIstrekNovi please see my edited answer. I have addressed all the valid points that miken32 has raised.

– Nick
Nov 27 '18 at 2:11





@KikiFIstrekNovi please see my edited answer. I have addressed all the valid points that miken32 has raised.

– Nick
Nov 27 '18 at 2:11


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490631%2fphp-str-replace-and-preg-replace-not-working-with-html%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen