haku Posted December 4, 2008 Share Posted December 4, 2008 I need to strip the space from the text inside an anchor tag, and replace it with a <br /> tag. I am using this code right now: $link = '<a href="this/is.html">This is a link</a>'; $text = strip_tags($link); $converted_text = str_replace(' ','<br />',$text); echo str_replace($text,$converted_text,$link); It works just fine. But I'm thinking there should be a way to simplify it into using one function using regex, or maybe some other way. But I'm crap at regex and I can't think of another way. Anybody want to take a stab as simplifying it? Link to comment https://forums.phpfreaks.com/topic/135444-simplifying-a-function/ Share on other sites More sharing options...
.josh Posted December 4, 2008 Share Posted December 4, 2008 <?php $link = '<a href="this/is.html">This is a link</a>'; $link = preg_replace("/(?<!<a)\s/","<br />",$link); echo $link; ?> edit: regex uses a negative look behind to match and replace any space that is not preceded by a '<a' Link to comment https://forums.phpfreaks.com/topic/135444-simplifying-a-function/#findComment-705645 Share on other sites More sharing options...
haku Posted December 4, 2008 Author Share Posted December 4, 2008 Nice, thanks. That's exactly what I was looking for. Nice little regex - I'm going to spend a bit of time looking at how it works. Regex is something I've been trying to pick up for a while, but I'm still not very good at it. Link to comment https://forums.phpfreaks.com/topic/135444-simplifying-a-function/#findComment-705682 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.