Lion King
Lion King

Reputation: 33813

cut a string from position to another position

Firstly: I have the following string content

Autumn, tree leafs<BR/><BR/><a title='Autumn, tree' href='http://site.net/share-picture/3K'><img width='225px' src='http://site.net/_s/upload/2013/11/29/0be99469a67aaf35b30b236e8ee9faa3.jpg225.jpg' title='Autumn, tree leafs' alt='Autumn, tree leafs'/></a><br/>.

Now, what I want is cut part of that string, specifically http://site.net/_s/upload/2013/11/29/0be99469a67aaf35b30b236e8ee9faa3.jpg225.jpg, that affiliated to src tag.

What I done is:

function imgSrc($content){ 
    $srcPos = strpos($content, 'src');
    $srcCut = substr($content, $srcPos+5);

    return $srcCut;
}

The result of that function is http://site.net/_s/upload/2013/11/29/0be99469a67aaf35b30b236e8ee9faa3.jpg225.jpg' title='Autumn, tree leafs and big clock' alt='Autumn, tree leafs and big clock'/>.

But What I want is this only http://site.net/_s/upload/2013/11/29/0be99469a67aaf35b30b236e8ee9faa3.jpg225.jpg.

Upvotes: 1

Views: 101

Answers (2)

Jonas m
Jonas m

Reputation: 2734

You should check out regex. Will always work :)

$string = "Autumn, tree leafs<BR/><BR/><a title='Autumn, tree' href='http://site.net/share-picture/3K'><img width='225px' src='http://site.net/_s/upload/2013/11/29/0be99469a67aaf35b30b236e8ee9faa3.jpg225.jpg' title='Autumn, tree leafs' alt='Autumn, tree leafs'/></a><br/>";

$count = preg_match_all('/src=(["\'])(.*?)\1/', $string, $match);
if ($count === FALSE) {
    print 'Nada\n';
} else { 
    print_r($match[2]);
}

Upvotes: 0

Try like this

echo rtrim(strstr($str," title",true),"'");

i.e.

function imgSrc($content){ 
    $srcPos = strpos($content, 'src');
    $srcCut = substr($content, $srcPos+5);
    $srcCut = rtrim(strstr($srcCut," title",true),"'"); //Added here

    return $srcCut;
}

Upvotes: 1

Related Questions