I often need to isolate a certain part of a string, and am not good at regular expressions. Yes, I should get better at it, but for the meantime, I’d like to know what you feel is the best way to do the following.
I’ve got some random text, often tags, from which I need to isolate a fragment. Something like this:
<title>Ask a Question - Stack Overflow</title>
<link rel="shortcut icon" href="http://cdn.sstatic.net/stackoverflow/img/favicon.ico">
<link rel="apple-touch-icon" href="http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png">
<link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.sstatic.net/js/stub.js?v=a1714a379225"></script>
<link rel="stylesheet" type="text/css" href="http://cdn.sstatic.net/stackoverflow/all.css?v=bd607061c911">
From this, I need to get the URL of the “apple-touch-icon”. I would do this:
<?php
// $text contains the previous blurb
// This returns an array, with the second item starting with the necessary text
$explode = explode('apple-touch-icon" href=",$text);
// This returns the position of the `"`, which is what marks the end of the string
$end_of_string = strpos('"',$explode[1]);
// Now I get only this particular part of the string
$return = substr($explode,0,$end_of_string);
?>
Voila, it works! However, I am wondering if there is an easier/better way that does not involve regular expressions.
If you are parsing HTML, you should use something that parses HTML and not regex or string searching…that said, given your sample data and your desire to avoid regexs, you could just use strpos…I would prefer this over explode because it creates a pattern of usage that will convey your intent better.
int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )