I have a image with class 'attachment-fullslideshow'. The code is
<img class="attachment-fullslideshow" src="demo.jpg">
I would like to replace the class from 'attachment-fullslideshow' to 'attachment-fullslideshow quote'.
Please suggest.
You could do something like this:
$str = '<img class="attachment-fullslideshow" src="demo.jpg">';
$str = str_replace('class="attachment-fullslideshow', 'class="attachment-fullslideshow quote', $str);
//Result: <div class="attachment-fullslideshow quote">...</div>
Or with regular expressions:
$str = '<img class="attachment-fullslideshow" src="demo.jpg">';
$str = preg_replace(':class="(.*attachment-fullslideshow.*)":', 'class="\1 quote"', $str);
//Result: <div class="defaultClass myClass">...</div>
Hope this helps!
You could also
$html = '<img class="attachment-fullslideshow" src="demo.jpg">';
$test = preg_replace('/class="(.*?)"/s', 'class="newclass"', $html);
echo $test;
outputs
<img class="newclass" src="demo.jpg">
Related
I'm using this code
echo preg_replace('#\{{2}(.*?)\}{2}#', '<img src="/base/$1">', $input);
To display an input like this:
foo foo {{picture.jpg}} foo
In img html tag.
I would like to add an optional alt attribute in this format:
{{picture.jpg|optional}}
and translate into:
<img src="/base/$1" alt="$2">
only if it is present, without make another preg_replace but only improving the existing one.
How can i do this?
You could use
{{(.+?)(?:\|([^}]+))?}}
See a demo on regex101.com.
Which in PHP, with the help of preg_replace_callback() would be:
<?php
$string = <<<DATA
foo foo {{picture.jpg}} foo
{{picture.jpg|optional}}
DATA;
$regex = '~{{(.+?)(?:\|([^}]+))?}}~';
$string = preg_replace_callback($regex,
function($match) {
if (!empty($match[2])) {
return "<img src='/base/{$match[1]}' alt='{$match[2]}'>";
} else {
return "<img src='/base/{$match[1]}'>";
}
}, $string);
echo $string;
This yields
foo foo <img src='/base/picture.jpg'> foo
<img src='/base/picture.jpg' alt='optional'>
Since you only forbid to use another preg_replace not another regex this works:
preg_replace(['#\{{2}(.*?)\|(.*?)\}{2}#', '#\{{2}(.*?)\}{2}#'], ['<img src="/base/$1" alt="$2">', '<img src="/base/$1">'], $input);
Working example:
function replacer($input) {
return preg_replace(['#\{{2}(.*?)\|(.*?)\}{2}#', '#\{{2}(.*?)\}{2}#'], ['<img src="/base/$1" alt="$2">', '<img src="/base/$1">'], $input);
}
echo replacer('{{picture.jpg}}');
echo replacer('{{picture.jpg|optional}}');
Output:
<img src="/base/picture.jpg">
<img src="/base/picture.jpg" alt="optional">
I have a string like this:
'<img src="image1.jpg"><img src="image2.jpg"><img src="image3.jpg">
<img src="image4.jpg"><img src="image5.jpg">'
I'd like to come to:
'<div class="slide"><img src="image1.jpg"><img src="image2.jpg"></div>
<div class="slide"><img src="image2.jpg"><img src="image3.jpg"></div>
<div class="slide"><img src="image3.jpg"></div>'
So basically I'd like to apply a preg_replace to wrap every 2 elements in a DIV.
I searched the forum and found some tips that I tried:
$pattern = '/(<img[^>]*class=\"([^>]*?)\"[^>]*>)+/i';
$replacement = '<div class="slide">$1</div>';
$content = preg_replace($pattern, $replacement, $content);
and
$pattern = '/(<img[^>]*class=\"([^>]*?)\"[^>]*>){2,}/i';
$replacement = '<div class="slide">$1</div>';
$content = preg_replace($pattern, $replacement, $content);
But it doesn't work...
Any idea, guys?
Thank you!
Use DomDocumen object to do it:
$str = '<img src="image1.jpg"><img src="image2.jpg"><img src="image3.jpg">
<img src="image4.jpg"><img src="image5.jpg">';
$dom = new DomDocument;
$dom->loadHTML($str);
$imgs = $dom->getElementsByTagName('img');
$i = $imgs->length;
$cur = 0;
$res = new DomDocument;
while ($i >= 2) {
$div = $res->createElement('div');
$div->setAttribute("class","slide");
$res->appendChild($div);
$div->appendChild($res->importNode($imgs->item($cur++)));
$div->appendChild($res->importNode($imgs->item($cur++)));
$i -= 2;
}
if($i) $res->appendChild($res->importNode($imgs->item($cur++)));
echo $res->saveHTML();
// <div class="slide"><img src="image1.jpg"><img src="image2.jpg"></div><div class="slide"><img src="image3.jpg"><img src="image4.jpg"></div><img src="image5.jpg">
Here is working example:
<?php
$content = '<img src="image1.jpg"><img src="image2.jpg">
<img src="image3.jpg"><img src="image4.jpg">
<img src="image5.jpg">';
$pattern = '/((<img[^>]*src=\"([^>]*?)\"[^>]*>\s*){2,2})/i';
$replacement = '<div class="slide">$1</div>';
$content = preg_replace($pattern, $replacement, $content);
echo $content;
It is your second example, but with 4 fixes:
class -> src
added block () to catch 2 items
fixed quantity {2,} -> {2,2} (change it to {1,2} if you want to catch last one img tag)
\s* added to catch spaces and line breaks between img tags
Mission is to replace all <img> tags in given string with <div> tags and src property as inner text.
In search for the answer I found similar question
<?php
$content = "this is something with an <img src=\"test.png\"/> in it.";
$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);
echo $content;
?>
result:
this is something with an (image) in it.
Question: How to upgrade script ant get this result:
this is something with an <div>test.png</div> in it.
This is the kind of problem that PHP's DOMDocument class excels at:
$dom = new DOMDocument();
$dom->loadHTML($content);
foreach ($dom->getElementsByTagName('img') as $img) {
// put your replacement code here
}
$content = $dom->saveHTML();
$content = "this is something with an <img src=\"test.png\"/> in it.";
$content = preg_replace('/(<)([img])(\w+)([^>]*>)/', '<div>$1</div>', $content);
echo $content;
<?php
$strings = 'awdaw <img src="http://ua1.us/media/media.jpg" alt="Image" width="100" height="100"> aw <img src="http://ua1.us/media/media1awdwa.jpg"> wawadwad';
preg_match_all('/<img[^>]+>/i', $strings, $images);
foreach ($images[0] as $image) {
preg_match('/src="([^"]+)/i', $image, $replacements);
$replacement = isset($replacements[1]) ? $replacements[1] : (isset($replacements[0]) ? $replacements[0] : "image");
$strings = str_replace($image, $replacement, $strings);
}
echo $strings;
Here is my code:
$search = array('<script src="/',
'<link href="/',
'<a href="/',
'<img src="/',
'src="/');
$d = 'http://www.ifreewind.net';
$replace = array('<script src="'.$d.'/',
'<link href="'.$d.'/',
'<a href="'.$d.'/',
'<img src="'.$d.'/',
'src="'.$d.'/');
$result = str_replace($search, $replace, $contents);
echo $result;
These code have a problem is that they cannot replace img tag such as :
<img width="50px" src="/...">
into
<img width="50px" src="http://www.ifreewind.net/...">
How to fix that?
You can't use str_replace for this. You could try it with preg_replace:
preg_replace('~(src|href)="(?=/)~', '$1http://www.ifreewind.net', $contents);
However, I'd strongly advise you to use an HTML parser instead.
I have a string <div id="myid">...</div>
How would I change this into
<div id="myid">...</div>
I have tried a few things but no luck any help?
Update
function get_page(){
$file = 'file.php';
$str = file_get_contents($file);
$str = html_entity_decode($str);
return $str;
}
$output = get_page();
echo $output;//don't work
FIX
function get_page(){
$file = 'file.php';
$str = file_get_contents($file);
return $str;
}
$output = get_page();
echo html_entity_decode($output);//works
The function for that is htmlspecialchars_decode().
Note that for the function to decode quotes, you need to specify the $quote_style parameter.
html_entity_decode is what you need: http://www.php.net/manual/en/function.html-entity-decode.php
$from = array('<', '>');
$to = array('<', '>');
$string = str_replace($from, $to, $string);
use this it's better ...
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> Other text';
echo strip_tags($text);
echo "\n";
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
htmlspecialchars_decode($string)