karldesign Posted November 7, 2007 Share Posted November 7, 2007 I have the following function to parse URLs in a string: function parseUrl($input){ $output = preg_replace("/(http:\/\/|https:\/\/)([^\s,]*)/i","<a href='$1$2' target='_blank' rel='nofollow'>$2</a>",$input); return $output; } Where the output is $2, I would very much like to shorten that to 20 chars, any ideas on how I could go about doing this? Quote Link to comment https://forums.phpfreaks.com/topic/76390-url-replace/ Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2007 Share Posted November 7, 2007 try this parseUrl($input){ $output = preg_replace("/(http:\/\/|https:\/\/)([^\s,]{1,20})/i","<a href='$1$2' target='_blank' rel='nofollow'>$2</a>",$input); return $output; } Quote Link to comment https://forums.phpfreaks.com/topic/76390-url-replace/#findComment-386768 Share on other sites More sharing options...
karldesign Posted November 7, 2007 Author Share Posted November 7, 2007 Ah, problem, where my $1$2 are together, they need to remain full, as the url... just the output text needs to be shrunken. Quote Link to comment https://forums.phpfreaks.com/topic/76390-url-replace/#findComment-386786 Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2007 Share Posted November 7, 2007 oh here you go function parseUrl($input){ $output = preg_replace("/(http:\/\/|https:\/\/)([^\s,]*)/i","<a href='$1$2' target='_blank' rel='nofollow'>$2</a>",$input); return substr($output,0,20); } Quote Link to comment https://forums.phpfreaks.com/topic/76390-url-replace/#findComment-386789 Share on other sites More sharing options...
Demonic Posted November 7, 2007 Share Posted November 7, 2007 oh here you go function parseUrl($input){ $output = preg_replace("/(http:\/\/|https:\/\/)([^\s,]*)/i","<a href='$1$2' target='_blank' rel='nofollow'>$2</a>",$input); return substr($output,0,20); } I highly doubt that would work since thats shortening the whole url instead of the actual output of the url. function parseUrl($input){ $output = preg_replace("/(http:\/\/|https:\/\/)([^\s,]*)/i","<a href='$1$2' target='_blank' rel='nofollow'>substr($2,0,20)</a>",$input); return $output; } Don't know if that would work, but give it a try.. Quote Link to comment https://forums.phpfreaks.com/topic/76390-url-replace/#findComment-386802 Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2007 Share Posted November 7, 2007 ok fine here you go return substr($output,0,28); I did not know I had to think for other people it was just a suggestion Demonic basically I give insite on other people I do not write code for them if you have anything better to suggest please do otherwise do not quote it Quote Link to comment https://forums.phpfreaks.com/topic/76390-url-replace/#findComment-386804 Share on other sites More sharing options...
karldesign Posted November 7, 2007 Author Share Posted November 7, 2007 return substr($output,0,28); Anything returned is shortened, thus the full string is shortened... if you can imagine this function is crawling though text in a few paragraphs... The substr within the preg_replace just outputs substr as characters... I also tried breaking the line like so: <a... >".substr($2)."... but again, this did not work Quote Link to comment https://forums.phpfreaks.com/topic/76390-url-replace/#findComment-386820 Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2007 Share Posted November 7, 2007 my apologies to everyone still looking into it, I see my errors (warnings) in my system Quote Link to comment https://forums.phpfreaks.com/topic/76390-url-replace/#findComment-386827 Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2007 Share Posted November 7, 2007 parseUrl($input){ $output = ""; if (preg_match("/(http:\/\/|https:\/\/)([^\s,])/i",$input,$arrMatches)) { $output = "<a href='".$arrMatches[0][0].$arrMatches[0][1]."' target='_blank' rel='nofollow'>".substr(0,20,$arrMatches[0][2])."</a>" } return $output; } please play with the $arrMatches I do not have a server with php on my machine right now Quote Link to comment https://forums.phpfreaks.com/topic/76390-url-replace/#findComment-386830 Share on other sites More sharing options...
Demonic Posted November 7, 2007 Share Posted November 7, 2007 $arrMatches[0][0] should be $arrMatches[0] Quote Link to comment https://forums.phpfreaks.com/topic/76390-url-replace/#findComment-386850 Share on other sites More sharing options...
karldesign Posted November 7, 2007 Author Share Posted November 7, 2007 no, this just returns emptiness... Quote Link to comment https://forums.phpfreaks.com/topic/76390-url-replace/#findComment-386855 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.