0

I have a string that contains the following:

<img data-bind="defaultSrc: {srcDesktop: 'http://desktoplink', srcMobile: 'http://mobilelink', fallback: 'http://baseurl'}" >

I am trying to extract the srcDesktop contained inside the string. I want my final result to yield me with the link http://desktoplink. What is the best way to achieve that other than str_replace? I have a dataset that contains those strings so I am looking for a formula to extract it in php.

Here is how I have been doing it, but there is got to be a more efficient way:

$string = '<img data-bind="defaultSrc: {srcDesktop: \'http://desktoplink\', srcMobile: \'http://mobilelink\', fallback: \'http://baseurl\'}" >';
$test = explode(" ",$string);
echo "<br>".str_replace(",","",str_replace("'","",$test['3']));
3
  • 2
    Your string is invalid; it will read "<img data-bind=" and then throw a syntax error. You need to escape your quotation marks. Commented Sep 11, 2017 at 23:11
  • @ObsidianAge I will fix.. just noticed that Commented Sep 11, 2017 at 23:11
  • @shnisaka I have updated my answer. Added "i" modificator for case insensitivity and \b to determine srcDesktop left word boundary. Commented Sep 12, 2017 at 1:25

2 Answers 2

1

You can use preg_match

$string = '<img data-bind="defaultSrc: {srcDesktop: \'http://desktoplink\', srcMobile: \'http://mobilelink\', fallback: \'http://baseurl\'}" >';

preg_match('/.*\bsrcDesktop:\s*(?:\'|\")(.*?)(?:\'|\").*/i', $string, $matches);

if (isset($matches[1])) {
    echo trim($matches[1]);
}
Sign up to request clarification or add additional context in comments.

Comments

1

you can use DOMDocument and json_decode to get this value, if you can change the code to the code below (added some '-signs):

$string = "<img data-bind=\"'defaultSrc': {'srcDesktop': 'http://desktoplink', 'srcMobile': 'http://mobilelink', 'fallback': 'http://baseurl'}\" >";
$doc = new DOMDocument();
$doc->loadHTML($string);
$data = str_replace('\'','"',$doc->getElementsByTagName('img')[0]->getAttribute('data-bind'));
$json = json_decode('{'.$data.'}');
var_dump($json->defaultSrc->srcDesktop);

4 Comments

Notice: Trying to get property of non-object in test.php on line 7 NULL
@shnisaka my bad. Fixed it. I missed the missing quotes at srcMobile and fallback. Is it an option for you to add them? I build json compatible syntax from your code... it would be better if you save a json string instead of something that looks a little bit like json. Then it's a little bit simpler ;)
your code is working now. Thank you for your contribution.. the method I am using is pulling faster at the moment. But I am willing to see other ideas. Yours is much more organized and can pull whatever you want with few changes.
@shnisaka yes. And it's tougher (safer). It will also work if there are more spaces or if srcDesktop is not the first noted key. What happens in your code if the url contains a comma? (it's valid in urls) You're perhaps changing the urls in the way you chose. and so on.

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.