Volkabulaire Posted September 30, 2012 Share Posted September 30, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/268931-giving-a-custom-name-to-http-preg_replace/ Share on other sites More sharing options...
berridgeab Posted September 30, 2012 Share Posted September 30, 2012 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/268931-giving-a-custom-name-to-http-preg_replace/#findComment-1381879 Share on other sites More sharing options...
Christian F. Posted September 30, 2012 Share Posted September 30, 2012 http\\3://\\1</a> Hmm... I wonder why you're getting "http://" in the replacement string. Also, you'll want to remove the \\3 from both locations in the replacement string, as there are no third sub group. If there was it would have malformed the URI making the link non-operative. Quote Link to comment https://forums.phpfreaks.com/topic/268931-giving-a-custom-name-to-http-preg_replace/#findComment-1381889 Share on other sites More sharing options...
Volkabulaire Posted September 30, 2012 Author Share Posted September 30, 2012 (edited) 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 September 30, 2012 by Volkabulaire Quote Link to comment https://forums.phpfreaks.com/topic/268931-giving-a-custom-name-to-http-preg_replace/#findComment-1381890 Share on other sites More sharing options...
Christian F. Posted September 30, 2012 Share Posted September 30, 2012 (edited) 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 September 30, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/268931-giving-a-custom-name-to-http-preg_replace/#findComment-1381893 Share on other sites More sharing options...
Volkabulaire Posted September 30, 2012 Author Share Posted September 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268931-giving-a-custom-name-to-http-preg_replace/#findComment-1381895 Share on other sites More sharing options...
Christian F. Posted September 30, 2012 Share Posted September 30, 2012 Why would you need to hide it, isn't it your code which determines how the HTML code is built? Quote Link to comment https://forums.phpfreaks.com/topic/268931-giving-a-custom-name-to-http-preg_replace/#findComment-1381897 Share on other sites More sharing options...
Volkabulaire Posted September 30, 2012 Author Share Posted September 30, 2012 Why would you need to hide it, isn't it your code which determines how the HTML code is built? No, what I meant is that I want to let the users input a html code, but I want to get that renamed/replaced by something else, which is also been done by users. How can I get it replaced? Quote Link to comment https://forums.phpfreaks.com/topic/268931-giving-a-custom-name-to-http-preg_replace/#findComment-1381911 Share on other sites More sharing options...
Christian F. Posted September 30, 2012 Share Posted September 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268931-giving-a-custom-name-to-http-preg_replace/#findComment-1381914 Share on other sites More sharing options...
Volkabulaire Posted September 30, 2012 Author Share Posted September 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268931-giving-a-custom-name-to-http-preg_replace/#findComment-1381923 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.