PHP: Filter all codes except scripts and images from MY site?
Lets say a member left a comment.
Hi! Look at these cars.
<img src="http://www.mysite.com/possiblefolder/possiblesub/image.jpg"></img>
<img src="http://othersite.com/possiblefolder/possiblesub/image.jpg"></img>
<img src="http://www.mysite.otherside.com/possiblefolder/image.jpg"></img>
Which is your favorite?
I want the results to come up as:
Hi! Look at these cars.
<img src="http://www.mysite.com/possiblefolder/possiblesub/image.jpg"></img>
http://othersite.com/possiblefolder/possiblesub开发者_运维技巧/image.jpg
http://www.mysite.otherside.com/possiblefolder/possiblesub/image.jpg
Which is your favorite?
I want to filter all codes except images and scripts coming from my site. Anyone got any ideas?
Hope this helps
<(\w+).+src=[\x22|'](?![^\x22']+mysite\.com[^\x22']+)([^\x22']+)[\x22|'].*>(?:</\1>)?
Group 1 is the tag used and group 2 is the "src" value so you can do a replace.
In Browser Demo
In most reasonable cases and in particular in your examples, this will work:
$new_comment = preg_replace('%<img.*?\ssrc="(http://(?!www.mysite.com).*?)".*?>.*?</img>%', '\1', $old_comment);
It will give the result you describe.
If it's not proper XHTML, run it through Tidy. If it's already clean XHTML, skip this part
$config = array('output-xhtml' => true);
$tidy = new tidy();
$html = $tidy->repareString($html, $config, 'utf8');
Now, having clean XHTML you can use XPath:
$xhtml = new SimpleXMLElement($html);
foreach ($xhtml->xpath('//*/img') as $img_parent) {
if(!(strpos($img_parent->img->src, 'http://www.mysite.com/') === 0)) {
$img_parent->img = new SimpleXMLElement($img_parent->img->src);
}
}
$cleaned_html = $xhtml->asXML();
You can use PHP strip_tags() to strip all HTML tags out from user-comment (highly recommended), also you need to implement some script code such as BBCode on PHPbb forums, etc...
[img]possibleimgdir/someimage.jpg[/img]
later search for [img] and [/img], append your root URL in front of content found between tags (example. http://www.mysite.com/possibleimgdir/someimage.jpg), check if file exists and then create HTML IMG tags for that SRC property if it is valid...
That's just one of possible ideas!
You could do it with a jQuery oneliner:
$('img:not(src^="http://www.mysite.com/")').hide()
精彩评论