Regex to replace url in html page - php

i have an html page
<tr>
<td rowspan="7">
<a href="http://www.link1.com/" style="text-decoration: none;">
<img src="image1.jpg" width="34" height="873" alt="" style="display:block;border:none" />
</a>
</td>
<td colspan="2" rowspan="2">
<a href='http://www.link1.com/test.php?c=1'>
<img src="image1.jpg" width="287" height="146" alt="" style="display:block;border:none" />
</a>
</td>
<td colspan="2" rowspan="2">
<a href='http://www.url.com/test.php?c=1'>
<img src="image1.jpg" width="287" height="146" alt="" style="display:block;border:none" />
</a>
</td>
I want to replace all url in href by mytest.com?url=$link
I try with :
$messaget = preg_replace('/<a(.*)href="([^"]*)"(.*)>/','mytest.com?url=$2',$messaget);

This may help you in the short run:
preg_replace('/<a (.*)href=[\'"]([^"]*)[\'"](.*)>/', '<a $1href="mytest.com?url=$2"$3>', $messaget);
In your regex you were using href="...", that is, double quotes, but in your HTML you have a mixture of both double and single quotes.
And in the replacement string you forgot to include $1 and $3.
That said, DO NOT use regex to parse HTML. The answer by #BenLanc below is better, use that instead. Read the link he posted.

Don't use regex on HTML, HTML is not regular.
Assuming your markup is valid (and if it's not, pass it through Tidy first), you should use xpath, to grab the elements and then update the href directly. For example:
<?php
$messaget = <<<XML
<tr>
<td rowspan="7">
<a href="http://www.link1.com/" style="text-decoration: none;">
<img src="image1.jpg" width="34" height="873" alt="" style="display:block;border:none" />
</a>
</td>
<td colspan="2" rowspan="2">
<a href='http://www.link1.com/test.php?c=1'>
<img src="image1.jpg" width="287" height="146" alt="" style="display:block;border:none" />
</a>
</td>
<td colspan="2" rowspan="2">
<a href='http://www.url.com/test.php?c=1'>
<img src="image1.jpg" width="287" height="146" alt="" style="display:block;border:none" />
</a>
</td>
</tr>
XML;
$xml = new SimpleXMLElement($messaget);
// Select all "a" tags with href attributes
$links = $xml->xpath("//a[#href]");
// Loop through the links and update the href, don't forget to url encode the original!
foreach($links as $link)
{
$link["href"] = sprintf("mytest.com/?url=%s", urlencode($link['href']));
}
// Return your HTML with transformed hrefs!
$messaget = $xml->asXml();

Don't forget /m at the end of your regexp since your are using multiline source:
PHP Doc PCRE

Regex to match an url:
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
More background info

Related

Style an echoed image

it seems like I cannot style my echoed image in php. How can I do this? Thanks :)
<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'" height="80px" width="80px" class="img-thumnail" style="margin-bottom:90px;"/>
Try This code
<img class="im" src="data:image/jpeg;base64,'<?php echo base64_encode($row['image'] ); ?>'" height="180px" width="180px" class="img-thumnail" style="margin-bottom:90px;"/>

retrieve all images path from string with PHP

How do I get all images path from a string?
Note I just want the path containing the word "media".
For example given this string (part of the DOM)
<div class="my-class">
<img src="http://my-website.com/cache/media/2017/10/img67.jpeg" class="" alt="test" width="120" height="100">
<img src="http://my-website.com/cache/2017/10/img68.png" class="" alt="test" width="120" height="100">
<img src="http://my-website.com/cache/media/2017/10/img69.jpg" class="" alt="test" width="120" height="100">
<h2 class="uk-margin-top-remove">About us</h2>
</div>
I want an array containing a similar result:
array(
[0] => "http://my-website.com/cache/media/2017/10/img67.png"
[1] => "http://my-website.com/cache/media/2017/10/img69.png"
);
I don't want the second img because src attribute doesn't contain the word "media".
You could use preg_match_all() to get URLs but it is even better to use a DOM reader.
$str = '<div class="my-class">
<img src="http://my-website.com/cache/media/2017/10/img67.jpeg" class="" alt="test" width="120" height="100">
<img src="http://my-website.com/cache/2017/10/img68.png" class="" alt="test" width="120" height="100">
<img src="http://my-website.com/cache/media/2017/10/img69.jpg" class="" alt="test" width="120" height="100">
<h2 class="uk-margin-top-remove">About us</h2>
</div>' ;
$matches = [] ;
preg_match_all('~(http\://my-website\.com/cache/media/(.*?))"~i', $str, $matches) ;
var_dump($matches[1]);
Will returns :
array(2) {
[0]=>
string(52) "http://my-website.com/cache/media/2017/10/img67.jpeg"
[1]=>
string(51) "http://my-website.com/cache/media/2017/10/img69.jpg"
}
Some boilerplate code to get you started:
<?php
$data = <<<DATA
<div class="my-class">
<img src="http://my-website.com/cache/media/2017/10/img67.jpeg" class="" alt="test" width="120" height="100">
<img src="http://my-website.com/cache/2017/10/img68.png" class="" alt="test" width="120" height="100">
<img src="http://my-website.com/cache/media/2017/10/img69.jpg" class="" alt="test" width="120" height="100">
<h2 class="uk-margin-top-remove">About us</h2>
</div>
DATA;
# set up the dom
$dom = new DOMDocument();
$dom->loadHTML($data, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);# | LIBXML_COMPACT | LIBXML_NOENT );
# set up the xpath
$xpath = new DOMXPath($dom);
foreach ($xpath->query("//img[contains(#src, '/media/')]/#src") as $image) {
echo $image->nodeValue . "\n";
}
Which yields
http://my-website.com/cache/media/2017/10/img67.jpeg
http://my-website.com/cache/media/2017/10/img69.jpg
This loads the DOM and uses an xpath query for every image where we'll loop over afterwards.
If for some reasons (why?) you are unable to use a DOM parser, your could use the secondbest option:
<img
(?s:(?!>).)+?
src=(['"])
(?P<src>(?:(?!\1).)+?/media/.*?\1)
And use the src group, see a demo on regex101.com.

how to remove dots in php document which contains images

I would love to remove the Dots as you can see in the image Please Help
The Below is my code
<body>
<h1>Please Check Your Email For Verfication that you are not a Bot :-) </h1>
<a href='hotmail.com' class="icon" ><img src="outlook.png" width="70px" height="70px"/></a><br>;
<img src="Gmail.png" width="70px" height="70px"/><br>;
<img src="yahoo.png" width="70px" height="70px"/><br>;
</body>`
Perhaps removing the semi-colons could help:
<body>
<h1>Please Check Your Email For Verfication that you are not a Bot :-) </h1>
<a href='hotmail.com' class="icon" ><img src="outlook.png" width="70px" height="70px"/></a><br>
<img src="Gmail.png" width="70px" height="70px"/><br>
<img src="yahoo.png" width="70px" height="70px"/><br>
</body>
No need for the semicolons at the end of your lines, see below:
<body>
<h1>Please Check Your Email For Verfication that you are not a Bot :-)</h1>
<a href='hotmail.com' class="icon" ><img src="outlook.png" width="70px" height="70px"/></a><br>
<img src="Gmail.png" width="70px" height="70px"/><br>
<img src="yahoo.png" width="70px" height="70px"/><br>
</body>
If you'd like to review HTML syntax, this site is a great resource: HTML5 Syntax
So you have a few issues that are causing extra symbols to appear, and the links won't work as you'd expect.
The semi-colons are showing up because you have semi-colons after each <br/> which don't need to be there.
The underscores after each image are appearing because the images are nested in anchor tags. Anchor tags by default have an underline text decoration, and images have a small bit of extra sizing in most cases.
In anchor tags on almost all webservers, if you don't define a protocol (like http://) to use, then it will send the user to http://yoursite.com/<current_page>/<clicked_link> rather than the external website that you'd expect.
Using these should fix all of that:
HTML
<body>
<h1>Please Check Your Email For Verfication that you are not a Bot :-) </h1>
<a href="http://hotmail.com" class="icon" ><img src="outlook.png" width="70px" height="70px"/></a><br>
<img src="Gmail.png" width="70px" height="70px"/><br>
<img src="yahoo.png" width="70px" height="70px"/><br>
</body>
CSS
.icon {
text-decoration: none;
}

how to edit <img> tag in the string

<p> </p>
<p><img src="../../../..//js/uploads/images/selva.png" alt="" /></p>
<p>we are checkng our website</p>
<p>kfoaskf</p>
<p>asldfjas</p>
<p>jasdklfk'sd</p>
<p><img src="../../../..//js/uploads/images/images.jpg" alt="" /></p>
This is my string values. i need to change only this
<img src="../../../..//js/uploads/images/images.jpg"> to
<img src="js/uploads/images/images.jpg">
for every image ,i need to modify the path.
how to do it? and my string values also differ at every time
Try this bro
$str='<p> </p>
<p><img src="../../../..//js/uploads/images/selva.png" alt="" /></p>
<p>we are checkng our website</p>
<p>kfoaskf</p>
<p>asldfjas</p>
<p>jasdklfk\'sd</p>
<p><img src="../../../..//js/uploads/images/images.jpg" alt="" /></p>';
echo str_replace("../../../..//js/uploads/images/images.jpg","/js/uploads/images/images.jpg",$str);

Use a PHP variable in Javascript

How can I use a PHP variable inside the Javascript onclick=function?
<?php
for($i=0;$i<10;$i++){
echo '<li><div><img src='. $mobile_image_link[$i+$p] .' width="160" height="165" alt="" border="0" /></div></div></li>';
}
?>
Just concatenate the strings, and use backslashes to escape the necessary quotes:
echo '
<a href="#"
onclick="showPopUp(\''. $mobile_image_link[$i+$p].'\',
\''. $mobile_image_link[$i+$p].'\');">
<img src="'.$mobile_image_link[$i+$p].'" alt="" />
</a>'
Would result in:
<a href="#"
onclick="showPopUp('http://mylink.com/1',
'http://mylink.com/2');">
<img src="http://mylink.com/image.png" alt="" />
</a>
On a side note, please read the HTML latest specifications, setting tag style using width="", height="" and border="" is deprecated and discouraged
Simply echo php variable in javascript code:
<script type="text/javascript">
var js_variable= <?php echo $php_variable; ?>
</script>
Either use the double quotes approach (php variables are only inserted into double quote strings.
<?php for($i=0;$i<10;$i++){
echo "<li><div><img src=\"$mobile_image_link[$i+$p]\" width="160" height="165" alt="" border="0" /></div></div></li>";}
?>
or do what you already did for the img tag. You'll need to escape some quotes to make it work.
<?php for($i=0;$i<10;$i++){
echo '<li><div><img src="'.$mobile_image_link[$i+$p].'" width="160" height="165" alt="" border="0" /></div></div></li>';}
?>
<?php
for($i=0;$i<10;$i++){
$onclickvariable = "showPopUp('".$mobile_image_link[$i+$p]."','".$mobile_image_link[$i+$p]."');"
echo '<li><div><a href="#" onclick="'.$onclickvariable.'">
<img src='.$mobile_image_link[$i+$p].' width="160" height="165" alt="" border="0" /></a>
</div></li>';}
?>

Categories