needphphelp22 Posted February 7, 2011 Share Posted February 7, 2011 I need to remove some text from a php variable. right now $testv = <span id="sobi2Details_field_street">www.google.com</span> I need to remove the <span id="sobi2Details_field_street"> and the </span> so im left with $testv = www.google.com Link to comment https://forums.phpfreaks.com/topic/226986-removing-text-from-a-php-variable/ Share on other sites More sharing options...
Maq Posted February 7, 2011 Share Posted February 7, 2011 If the id is always the same you can use str_replace. If not, use preg_replace. Link to comment https://forums.phpfreaks.com/topic/226986-removing-text-from-a-php-variable/#findComment-1171101 Share on other sites More sharing options...
Psycho Posted February 7, 2011 Share Posted February 7, 2011 Assuming that you ONLY want the content in the SPAN tags and there is nothing outside the SPAN tags you need, then I'd use preg_match to just find the text within the SPAN tags. $testv = '<span id="sobi2Details_field_street">www.google.com</span>'; preg_match("#<span[^>]*>(.*)</span>#", $testv, $matches); $spanContent = $matches[1]; echo $spanContent; //Output: www.google.com Link to comment https://forums.phpfreaks.com/topic/226986-removing-text-from-a-php-variable/#findComment-1171106 Share on other sites More sharing options...
Psycho Posted February 7, 2011 Share Posted February 7, 2011 On second thought, preg_replace() probably would be easier $testv = '<span id="sobi2Details_field_street">www.google.com</span>'; $testv = preg_replace("#<span[^>]*>(.*)</span>#", "$1", $testv); echo $testv; //Output: www.google.com Link to comment https://forums.phpfreaks.com/topic/226986-removing-text-from-a-php-variable/#findComment-1171114 Share on other sites More sharing options...
needphphelp22 Posted February 7, 2011 Author Share Posted February 7, 2011 thanks a bunch, your awesome Link to comment https://forums.phpfreaks.com/topic/226986-removing-text-from-a-php-variable/#findComment-1171120 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.