php strip from text string
I have a string:
...<a href="http://mple.com/nCCK8.png">...
From this I am trying to strip out the
"nCCK8.png" part
I tried substr, but that requires 2 numbers and didn't work as it can be in different positions in the string. It does occur only once in the string.
The base string always has mple.com/ before th开发者_JS百科e nCCK8.png part, and always "> after.
What is the easiest way to do this?
[^\/]+?\.png
$_ = null;
if (preg_match('/([^\/]+?\.png)/i',$data,$_)){
echo $_[1];
}
Working Demo: http://www.ideone.com/3IkhB
Wow, all these other answers are so complex.
$tmp = explode('/', $string);
//if you actually WANT the "nCCK8.png" part
return substr($tmp[count($tmp) - 1], 0, -2);
//if you actually want the rest of it
$tmp = $array_pop($tmp);
return substr(implode('/', $tmp), 0, -2);
Unless the string is longer than you posted, and includes other slashes, this should work.
Get the href element via simplexml or DOM (see this answer) for instance then use parse-url to get the actual file, and finaly basename:
<?php
$href = 'http://mple.com/nCCK8.png';
$url_parts = parse_url($href);
echo basename($url_parts['path']);
?>
精彩评论