0
$var = '<img src="http://site.com/some_directory/filename.png"/>';

png can be replaced with any extension.

How to cut everything, except "filename" 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.

2 Answers 2

3
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).

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.