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.
Related
I've created a simple function that outputs an image with the correct html markup, seen below. The problem I am facing is when I pass into the alt text that has a space in it, such as "My cat is great", the alt breaks and shows alt="My" like <img src="blah.jpg" alt="My" cat is great class="home">. I'm having a hard time trying to figure this out. Any thoughts?
function image_creator($image_url, $alt=false, $class=false) {
$string = '<img src='.$image_url.' alt='.$alt.' class='.$class.'>';
return $string;
}
You're not actually outputting <img src="blah.jpg" alt="My" cat is great class="home"> - that's just how the browser is interpreting it.
You're outputting <img src=blah.jpg alt=My cat is great class=home>
You need to output some quotes:
$string = '<img src="'.$image_url.'" alt="'.$alt.'" class="'.$class.'">';
$string = "<img src='".$image_url."' alt='".$alt."' class='".$class."'>';
try this i think it will work
A little user failure. No problem!
Make sure you have your <img> tag correctly.
$image_url = 'https://example.com/image';
$alt = 'My cat is awesome';
$class = 'image';
So:
function image_creator($image_url, $alt=false, $class=false) {
$string = '<img src='.$image_url.' alt='.$alt.' class='.$class.'>';
return $string;
}
Outcome:
<img src="https://example.com/image" alt="My" cat="" is="" awesome="" class="image">
Should be:
function image_creator($image_url, $alt=false, $class=false) {
$string = '<img src="'.$image_url.'" alt="'.$alt.'" class="'.$class.'">';
return $string;
}
Outcome:
<img src="https://example.com/image" alt="My cat is awesome" class="image">
Do you see the differences? Look carefully at the quotes.
Documentation:
http://php.net/manual/en/language.types.string.php
Some examples about using quotes:
https://www.virendrachandak.com/techtalk/php-double-quotes-vs-single-quotes/
Personally I would like to use:
// $image below is being sent to the function
$image = [
'image_url' => 'https://example.com/image',
'alt' => 'My cat is awesome',
'class' => 'image',
];
function image_creator($image = [])
{
if(empty($image))
{
return 'No image';
}
return '<img src="' . $image['image_url'] . '" alt="' . $image['alt'] . '" class="' . $image['class'] . '">';
}
//Multiple solution with multiple way
$image_url = 'https://example.com/image';
$alt = 'My cat is awesome';
$class = 'image';
$string = '<img src="'.$image_url.'" alt="'.$alt.'" class="'.$class.'">';
//also you can direct echo
echo '<img src="',$image_url,'" alt="',$alt,'" class="',$class,'">';
//also you can direct echo
$string= "<img src='{$image_url}' alt='{$alt}' class='{$class}' >";
//also you can direct echo
$string = "<img src='${image_url}' alt='${alt}' class='${class}' >";
echo $string;
So I have this php variable:
$keywords = array("google"=>"http://google.com","stackoverflow"=>"http://stackoverflow.com");
$content = "<p>Hello Stackover flow</p>
<p><img src='IMG_URL' alt='stackoverflow logo'/></p>
<p>Let's go to <a href='http://stackoverflow.com'>stackoverflow</a></p>
<p>To use stackoverflow and google</p>";
How can I convert it to that:
$content = "<p>Hello Stackover flow</p>
<p><img src='IMG_URL' alt='stackoverflow logo'/></p>
<p>Let's go to <a href='http://stackoverflow.com'>stackoverflow</a></p>
<p>To use google</p>";
Suggest you to use array_map() & implode(). Example:
$keywords = array("google"=>"http://google.com","stackoverflow"=>"http://stackoverflow.com");
$content = "<p>Hello Stackover flow</p>
<p><img src='IMG_URL' alt='stackoverflow logo'/></p>
<p>Let's go to <a href='http://stackoverflow.com'>stackoverflow</a></p>
<p>To use stackoverflow and google</p>";
$anchors = array_map(function($v, $k){
return ''.$k.'';
}, $keywords, array_keys($keywords));
$anchors = '<p> To use '.implode(' and ', $anchors).'</p>';
echo preg_replace("/<p>To use (.*?)<\/p>/", $anchors, $content);
Reference:
array_map()
implode()
preg_replace()
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">
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;
Hello how can I delete anything of this string and have only the XBH1dcHoL6Y ?
<param name="movie" value="http://www.youtube.com/v/XBH1dcHoL6Y&rel=0&hl=en_US&feature=player_embedded&version=3">
My purpose is to have the url like this
http://www.youtube.com/watch?v=XBH1dcHoL6Y
and this is what I found so far (but i can't delete the parameters)
$url = "http://www.youtube.com/v/6n8PGnc_cV4";
$start = strpos($url,"v=");
echo 'http://www.youtube.com/v/'.substr($url,$start+2);
Thank you!
Obligatory one-liner:
echo end(explode('/', reset(explode('&', 'http://www.youtube.com/v/XBH1dcHoL6Y&rel=0&hl=en_US&feature=player_embedded&version=3'))));
Edit: preg_match version:
$string = '<embed src="http://www.youtube.com/v/XBH1dcHoL6Y&rel=0&hl=en_US&feature=player_embedded&-version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="390"></embed>';
$expr = "/<embed.*https?:\/\/www.youtube.com\/v\/([a-zA-Z0-9]+).*<\/embed>/";
if(preg_match($expr, $string, $matches))
echo 'Matched: '.$matches[1];
else
echo 'No match';
// returns "Matched: XBH1dcHoL6Y"
try this:
$url = "http://www.youtube.com/v/XBH1dcHoL6Y&rel=0&hl=en_US";
$urlSplode = explode('/',$url);
$urlEnd = $urlSplode[count($urlSplode)-1];
$urlEndSplode = explode('&',$urlEnd);
$finalStr = $urlEndSplode[0];
echo "http://www.youtube.com/watch?v=$finalStr";
here is the demo:
http://codepad.org/aoGLZSX0
$urlTokens = explode("/", "http://www.youtube.com/v/XBH1dcHoL6Y&rel=0&hl=en_US&feature=player_embedded&version=3");
$urlTokens = explode("&", $urlTokens[4]);
$url = $urlTokens[0]; // will output XBH1dcHoL6Y