I have a string containing a section of text that I want to extract from a variable. In the line below, I would like to extract something with /in it.
$str = 'this is a random string foo/bar random - string words';
In the above example, I would like to extract foo/bar. I am currently doing this with a explodingstring in spaces, and then iterate over and check to see if each section contains /.
$words = explode(' ', $str);
foreach($words as $word) {
if(strpos($word, '/') !== false) {
$myVar = $word;
}
}
Is there a beeter way to do this since I need to do this for a lot of text strings?
user5483305
source
share