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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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