Jump to content

url replace


karldesign

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/76390-url-replace/
Share on other sites

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..

Link to comment
https://forums.phpfreaks.com/topic/76390-url-replace/#findComment-386802
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/76390-url-replace/#findComment-386804
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/76390-url-replace/#findComment-386820
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/76390-url-replace/#findComment-386830
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.