Jump to content

Giving A Custom Name To Http (Preg_Replace)


Volkabulaire

Recommended Posts

I have a function with a button, that fetches a javascript pop-up with an input where people can post a hyperlink to another website. That works, but now, I want to give the hyperlink a different name, other than the linkname itself.

 

$message = preg_replace("'\(http://(.*)\)'Ui","<a target=blank_ href=\"http\\3://\\1\">http\\3://\\1</a>",$message);

 

This is what I want to happen:

 

http://i.stack.imgur.com/lcMgH.png

 

So you can see that 1 2 and 3 are links. On my site, you can see the links too, but the full http:// path of the link is shown and not a custom name. How can I make this happen?

Link to comment
Share on other sites

Your question isn't very clear.

 

Where is the replacement name for the link coming from? The user, or is PHP mean't to generate it?

 

If you already have the raw link from the user its just a case of

 

echo "<a href = '$linkFromUser' target = '_blank'>My Link Title with no HTTP</a>";

Link to comment
Share on other sites

Well, alright, let me rephrase it:

 

There is no replacement name for the link (beside the fact that is says http://). A user clicks on the button, a javascript pop-up pops out, the user must insert a valid URL, then, this is entered in the textarea automatically. If they post it, you can see the full URL. Now, I want to make it possible to rename this URL. I guess it is logical to have a second javascript pop-up which says: "what will be your url name replacement" or something else.

 

$message = preg_replace("'\(http://(.*)\)'Ui","<a target=blank_ href=\"http\\3://\\1\">click here</a>",$message);

 

 

I could make this, but this is standard and not renamed by the user. I want it to be renamed by the user.

Edited by Volkabulaire
Link to comment
Share on other sites

I'd have a second input element in that same pop-up, asking for the link text right away. Makes it clear straight up what is necessary, and what the result will be.

In this case you don't even need preg_replace (), just normal string concatenation.

 

You might need to alter the table you're saving this in as well, to add another field for the link text. Unless you're storing the HTML code directly in it, which wouldn't be the best of solutions (in my opinion).

Edited by Christian F.
Link to comment
Share on other sites

I'd have a second input element in that same pop-up, asking for the link text right away. Makes it clear straight up what is necessary, and what the result will be.

In this case you don't even need preg_replace (), just normal string concatenation.

 

You might need to alter the table you're saving this in as well, to add another field for the link text. Unless you're storing the HTML code directly in it, which wouldn't be the best of solutions (in my opinion).

 

But how can I hide the full url path and replace it with the user generated text automatically? It does not make sense that a input field will solve it all.

Link to comment
Share on other sites

Why not just limit them to the URL and the link text, just like the link button works in most forum solutions? It would make the situation a lot simpler, for both you and your users. That'll leave you with only one string with two variable inputs, pure and simple.

 

Hmm, I didn't know it was that simple... Could you give a code example? I don't know where to start really, especially now PHP and all other languages are mixed together.

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.