hello I have the following tags :
$content ='<a href="http://website.com/" />
<a href="/link1" />
<a href="https://website.com" />
<a href="link1" />';
and this code :
preg_replace('~href=(\'|"|)(.*?)(\'|"|)(?<!\/|http:\/\/|https:\/\/)~i', 'href=$1http://website2.com/$2$3', $content);
I want to use the code above to replace href tags doesn't start with an http or https or with a slash . thanks in advance.
Something like this should do it. I'd advise using a parser in the future, How do you parse and process HTML/XML in PHP?, for tasks such as this though. This can become very messy quickly. Here's a link on this regex usage as well, http://www.rexegg.com/regex-best-trick.html.
Regex:
/href=("|')https?:\/\/(*SKIP)(*FAIL)|href=("|')(.*?)\2/
Demo: https://regex101.com/r/cV2xB5/1
PHP Usage:
$content ='<a href="http://website.com/" />
<a href="/link1" />
<a href="https://website.com" />
<a href="link1" />';
echo preg_replace('/href=("|\')https?:\/\/(*SKIP)(*FAIL)|href=("|\')\/?(.*?)\2/', 'href=$2http://website2.com/$3$2', $content);
Output:
<a href="http://website.com/" />
<a href="http://website2.com/link1" />
<a href="https://website.com" />
<a href="http://website2.com/link1" />
Update, for // exclusion
Use:
href=("|')(?:https?:)?\/\/(*SKIP)(*FAIL)|href=("|')(.*?)\2
Demo: https://regex101.com/r/cV2xB5/2
PHP:
$content ='<a href="//website.com/" />
<a href="/link1" />
<a href="https://website.com" />
<a href="link1" />';
echo preg_replace('/href=("|\')(?:https?:)?\/\/(*SKIP)(*FAIL)|href=("|\')\/?(.*?)\2/', 'href=$2http://website2.com/$3$2', $content);
Output:
<a href="//website.com/" />
<a href="http://website2.com/link1" />
<a href="https://website.com" />
<a href="http://website2.com/link1" />
Related
I've got a problem with my little text to smiley/emoji function in php, which is based on str_replace. This is my code.
$smileys = array( ":inlove:" => "/smileys/smiley2.png",
":cool:" => "/smileys/smiley3.png",
":tongue:" => "/smileys/smiley4.png",
":wow:" => "/smileys/smiley5.png",
":smile:" => "/smileys/smiley15.png",
":happy:" => "/smileys/smiley6.png",
":funny:" => "/smileys/smiley7.png",
":wink:" => "/smileys/smiley8.png",
":worried:" => "/smileys/smiley10.png",
":pokerface:" => "/smileys/smiley9.png",
":poop:" => "/smileys/smiley12.png",
":thinking:" => "/smileys/35_thinking.png",
":triumph:" => "/smileys/49_triumph.png",
":vulcan:" => "/smileys/109_vulcan.png",
":pointup:" => "/smileys/102_point_up_2.png",
":santa:" => "/smileys/135_santa.png",
":spy:" => "/smileys/134_spy.png");
if(isset($_POST['message'])) {
$messagePlain = $_POST['message'];
$messageSmileys = $messagePlain;
foreach($smileys as $key => $img) {
$messageSmileys = str_replace($key, '<img src="' . $img . '" />', $messageSmileys);
}
$connection->query(// Message to db);
}
It works fine. But the problem is, when the user inputs more than ~14 emojis in a row, the HTML gets destroyed and it looks like this:
And the HTML source like this:
<div class="media-body">
<h4 class="media-heading">test <small>07. August. 2017 01:34</small></h4>
<img src="/smileys/smiley2.png" /> <img src="/smileys/smiley3.png" /> <img src="/smileys/smiley5.png" /> <img src="/smileys/smiley4.png" /> <img src="/smileys/smiley15.png" /> <img src="/smileys/smiley6.png" /> <img src="/smileys/smiley7.png" /> <img src="/smileys/smiley8.png" /> <img src="/smileys/smiley10.png" /> <img src="/smileys/smiley9.png" /> <img src="/smileys/smiley12.png" /> <img src="/smileys/49_triumph.png" /> <img src="/smileys/109_vulcan.png" /> <img src="/smileys/102_point_up_2.p </div>
</div>
Could someone help me with this? Why is the HTML tag for <img> suddenly destroyed?
Looking at your output:
<img src="/smileys/smiley2.png" /> <img src="/smileys/smiley3.png" /> <img src="/smileys/smiley5.png" /> <img src="/smileys/smiley4.png" /> <img src="/smileys/smiley15.png" /> <img src="/smileys/smiley6.png" /> <img src="/smileys/smiley7.png" /> <img src="/smileys/smiley8.png" /> <img src="/smileys/smiley10.png" /> <img src="/smileys/smiley9.png" /> <img src="/smileys/smiley12.png" /> <img src="/smileys/49_triumph.png" /> <img src="/smileys/109_vulcan.png" /> <img src="/smileys/102_point_up_2.p
The above string's length is 499 characters. I strongly believe that your message field in the database table is limited to 500 characters or something and the output is truncated to those bits.
Solution / Suggestion
If you are using MySQL Database Server, I would strongly recommend you changing the database type from VARCHAR(500) to TEXT or LONGTEXT.
Your database field probably has a length limit. You might want to raise that, but there’s a more important initial fix: store the original text in the database and perform the replacements on the output instead.
This is especially important because it looks like you’re probably vulnerable to HTML injection right now. Make sure to run htmlspecialchars before doing the replacement and be aware of how you need to encode your output to make it safe. How to prevent XSS with HTML/PHP? might be a good start.
I have an input that consists of span opening and closing tag and in between if li opening and closing tags lie, I want to replace li tag with something. I have tried writing regex for it, you people can view it here:
https://regex101.com/r/vI1sY8/1 but it is replacing the li tag that lies outside the span tags as well.
Example my input:
lol <span style="background-color:limegreen;"> <br /> <li> this </li>
</span> <br /> this is something <li"> This is new </li>
My output should be:
lol <span style="background-color:limegreen;"> <br /> <li style="background-color:limegreen;"> this </li>
</span> <br /> this is something <li"> This is new </li>
This regex did the trick for me. Just swap second subgroup ($2) with whatever.
For example:
$html = 'lol <span style="background-color:limegreen;"> <br /> <li> this </li> </span> <br /> this is something <li"> This is new </li>';
$html = preg_replace('/(<span.+>.+)(<li>)(.+<\/li>.+<\/span>)/', '$1 replacement $3', $html);
result:
lol <span style="background-color:limegreen;"> <br /> replacement this </li> </span> <br /> this is something <li"> This is new </li>
Change replacement to whatever you need.
For Facebook and for Google i found this solution..
<script type="text/javascript">
$(document).ajaxComplete(function(){
try{
FB.XFBML.parse();
gapi.plusone.go();
}catch(ex){}
});
</script>
but not for tumblr. Does anybody know if there is any solution?
Tubmlr-post-button is implemented like this:
<a class="tumblr-share-button" data-href="http://www.xy.ch/news_entries/<? php echo "{$row['title']}.html" ?>" data-content="http://www.xy.ch/news_entries/<?php echo "{$row['title']}.html" ?>" data-color="blue" data-notes="right" href="https://embed.tumblr.com/share"></a>
Thanks
M
I had to find another solution (because i have another use case) and worked out this:
<img src='http://www.mywebsite.com/share_list.png' data-pin-no-hover="true" class="share_buttons_img" usemap='#share_buttons' />
<map name='share_buttons'>
<area shape='circle' coords='78,21,13,80' href="#" title="Share on Facebook" onclick="popUp=window.open('https://www.facebook.com/sharer/sharer.php?u=www.mywebsite.com/news/<?php echo "{$row['image_title']}.html" ?>','popupwindow', 'scrollbars=yes,width=500,height=400');popUp.focus();return false;" >
<area shape='circle' coords='116,21,13,80' href="#" title="Share on Tumblr" onclick="popUp=window.open('http://tumblr.com/widgets/share/tool?canonicalUrl=http://www.mywebsite.com/news/<?php echo "{$row['image_title']}.html" ?>','popupwindow', 'scrollbars=yes,width=500,height=400');popUp.focus();return false;" >
<area shape='circle' coords='153,21,13,80' href="#" title="Share on Google+" onclick="popUp=window.open('https://plus.google.com/share?url=www.mywebsite.com/news/<?php echo "{$row['image_title']}.html" ?>','popupwindow', 'scrollbars=yes,width=500,height=400');popUp.focus();return false;" >
</map>
The name of the column is : link. variable-type:varchar.
<img src="<?php echo$link;?>" style="width:300px; height:400px;" />
</html>
Your echo command could be missing an ";"
$link=$row['link'];
<img src="<?php echo $link; ?>" style="width:300px; height:400px;" />
Should work better.
It is the reverse of : PHP preg_replace: remove style=".." from img tags
Im trying to find an expression for preg_replace, that deletes all inline css styles except for images. For example, I have this text:
<img attribut="value" style="myCustom" attribut='value' style='myCustom' /> <input attribut="value" style="myCustom" attribut='value' style='myCustom'> <span attribut="value" style="myCustom" attribut='value' style='myCustom'> style= </span>
And I need to make it look like:
<img attribut="value" style="myCustom" attribut='value' style='myCustom' /> <input attribut="value" "myCustom" attribut='value' 'myCustom'> <span attribut="value" "myCustom" attribut='value' 'myCustom'> style= </span>
or like this:
<img attribut="value" style="myCustom" attribut='value' style='myCustom' /> <input attribut="value" attribut='value'> <span attribut="value" attribut='value'> style= </span>
It might looks like this
preg_replace('/(\<img[^>]+)(style\=\"[^\"]+\")([^>]+)(>)/', '${1}${3}${4}', $article->text)
The regex issue can be answered with a simple negative assertion:
preg_replace('/(<(?!img)\w+[^>]+)(style="[^"]+")([^>]*)(>)/', '${1}${3}${4}', $article->text)
And a simpler approach might be using querypath (rather than fiddly DOMDocument):
FORACH htmlqp($html)->find("*")->not("img") EACH $el->removeAttr("style");
This does as you've asked
$string = '<img attribut="value" style="myCustom" attribut=\'value\' style=\'myCustom\' /> <input attribut="value" style="myCustom" attribut=\'value\' style=\'myCustom\'> <span attribut="value" style="myCustom" attribut=\'value\' style=\'myCustom\'> style= </span>';
preg_match_all('/\<img.+?\/>/', $string, $images);
foreach ($images[0] as $i => $image) {
$string = str_replace($image,'--image'.$i.'--', $string);
}
$string = preg_replace(array('/style=[\'\"].+?[\'\"]/','/style=/'), '', $string);
foreach ($images[0] as $i => $image) {
$string = str_replace('--image'.$i.'--',$image, $string);
}
OUTPUTS
<img attribut="value" style="myCustom" attribut='value' style='myCustom' /> <input attribut="value" attribut='value' > <span attribut="value" attribut='value' > </span>
A very simple regular expression to get rid of them, without going too much into it would be
preg_replace( '/style="(.*)"/', 'REPLACEMENT' )
Very simple, effective enough though