开发者

PHP get from string

开发者 https://www.devze.com 2023-01-10 19:03 出处:网络
$var = \'<img src=\"http://site.com/some_directory/filename.png\"/>\'; png can be replaced with any extension.
$var = '<img src="http://site.com/some_directory/filename.png"/>';

png can be replaced with any extension.

How to cut everything, except "f开发者_StackOverflowilename" part?

And write into new variable. In this case:

$name = 'filename';

One more example:

$var = '<img src="http://site.com/directory/subdirectory/Pakahontos.txt"/>';
$name = 'Pakahontos';

Thanks.


preg_match('#([^/]+)\.\w+"#', $var, $matches);
$name = $matches[1];

Note that if $var is actually more complicated (e.g. has arbitrary HTML), you must use other methods (see e.g. DOMDocument).


Here's a basic HTML-parsing tutorial with preg_match. It's parsing images in the example.

0

精彩评论

暂无评论...
验证码 换一张
取 消