Input of email and url to be clickable - php

I have a form that I process in PHP. Users sometimes put their email address in the form or URLs. These usually come out as text after I strip the input of tags.
Recently my users started asking me to make their URLs and emails clickable when they pull up a page that displays their input (now pulled from a db).
Could someone please suggest a common pattern or ways that this is handled? Basically, if someone enters a url in a form, how do I make the url clickable instead of text when viewed?
Thanks,
Alex

You can use a regular expression based function like this
function autolink($message) {
//Convert all urls to links
$message = preg_replace('#([\s|^])(www)#i', '$1http://$2', $message);
$pattern = '#((http|https|ftp|telnet|news|gopher|file|wais):\/\/[^\s]+)#i';
$replacement = '$1';
$message = preg_replace($pattern, $replacement, $message);
/* Convert all E-mail matches to appropriate HTML links */
$pattern = '#([0-9a-z]([-_.]?[0-9a-z])*#[0-9a-z]([-.]?[0-9a-z])*\\.';
$pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
$replacement = '\\1';
$message = preg_replace($pattern, $replacement, $message);
return $message;

you have to wrap the url in a anchor tag:
assuming $myLink is the link text coming from your db:
<?php echo $myLink; ?>

Related

preg_match for subject and body of email

I need to extract subject/body of an email message generated by a form in Joomla.
I am using some tags for defining different subjects/body for each language, like:
{lang en}English Text{/lang}
{lang it}Italian Text{/lang}
{lang fr}French Text{/lang}
I have the following code for striping Text from Subjects in different languages:
$msgSubject = $template->subject;
if ($language=='it-IT')
{
preg_match('~{lang it}([^{]*){/lang}~i', $msgSubject, $match);
$msgSubject=($match[1]);
}
elseif ($language=='en-GB')
{
preg_match('~{lang en}([^{]*){/lang}~i', $msgSubject, $match);
$msgSubject=($match[1]);
}
and so on for other languages.
It works perfectly for the subjects of the emails, which is just plain text.
If I do the same on the Body, wich is a string containing HTML elemnes like p and br, and so on, it doesn't work....
Code sample:
$msgBody = $template->body;
if ($language=='it-IT')
{
preg_match('~{lang it}([^{]*){/lang}~i', $msgBody, $match);
$msgBody=($match[1]);
}
elseif ($language=='en-GB')
{
preg_match('~{lang en}([^{]*){/lang}~i', $msgBody, $match);
$msgBody=($match[1]);
}
it simply outputs nothing. What should I change in the preg_match function to avoid mass with HTML tags?
I have checked your code and it is working fine with html code also. But i think that $msgBody containing multiple tags that you are using with joomla.
so you should use below code
preg_match_all('~{lang it}([^{]*){/lang}~i', $msgBody, $match);
because preg_match match only first occurance.
Click on preg_match_all
Is this what you want? Works for me..
http://www.phpliveregex.com/p/fkB
Edit:
If you do like this:
http://www.phpliveregex.com/p/fkF
You capture the language too and you don't need the ifs.

Find URL in string, replace it and alter href

I've currently got a few DB entries which look like this:
1. This is some text http://www.sitehere.com more text
2. Text https://www.anothersite.com text text text
3. http://sitehere.com http://sitehereagain.com
4. Just text here blabla
I am trying to filter those entries while printing them and add infront of all the urls http://anothersite.com/?. Also put the new url destination as link but keep the original url as text:
text text http://sitehere.com text
Until now I've managed to add the http://anothersite.com/? part with the following code:
$result = preg_replace('/\bhttp:\/\/\b/i', 'http://anothersite.com/?http://', $input);
$result = preg_replace('/\bhttps:\/\/\b/i', 'http://anothersite.com/?https://', $input);
But the ahref is not the way I want it. Instead it is:
text text http://anothersite.com/?http://sitehere.com text
PS: I am not looking for a javascript solution :) Thank you!
This following code should work. There are a few large changes I made. The first one is I am using preg_replace_callback instead of preg_replace so I am able to properly encode the URL and have more control over the output. The other change is I'm matching the whole domain so the callback function can insert the URL between the <a> tags and also can add it to the hyperlink.
<?php
$strings = array(
'This is some text http://www.sitehere.com more text',
'Text https://www.anothersite.com text text text',
'http://sitehere.com http://sitehereagain.com',
'Just text here blabla'
);
foreach($strings as $string) {
echo preg_replace_callback("/\b(http(s)?:\/\/[^\s]+)\b/i","updateURL",$string);
echo "\n\n";
}
function updateURL($matches) {
$url = "http://anothersite.com/?url=";
return ''.$matches[1].'';
}
?>

PHP preg_replace: Need to modify hyperlink text if hyperlink contains 'youtube.com'

I'm almost there!
Here is the string I am trying to adjust, and my preg_replace attempt.
$description_string = '<p>Here is the test link: “Man or Muppet” with other text afterwards.</p>';
$description = preg_replace( '/(<a[^>]+youtube[^>]*>)+[^"]*(<\/a>)+/', '$0Watch This Video$2', $description );
The result I'm getting is incorrect:
Here is the test link: “Man or Muppet”Watch This Video with other text afterwards.
Any help would be greatly appreciated! THanks!
Well, not sure if you are trying to get the actual title in there. But here is what I came up with:
<?php
$description_string = '<p>Here is the test link: \'Man or Muppet\' with other text afterwards.</p>';
$description = preg_replace( '/(<a[^>]+youtube[^>]*>)+[^"]*(<\/a>)+/', '$1Watch This Video$2', $description_string );
echo $description;
?>
Result:
<p>Here is the test link: Watch This Video with other text afterwards.</p>
Your biggest issue was with the quotes (") being in the title. It is cutting off the anchor tags. While using $0 is also incorrect. You'll need to use $1.
This may not be exactly what you need, but its a quick monkey patch for you.

create anchors in a page with the content of <h2></h2> in PHP

Well I have a html text string in a variable:
$html = "<h1>title</h1><h2>subtitle 1</h2> <h2>subtitle 2</h2>";
so I want to create anchors in each subtitle that has with the same name and then print the html code to browser and also get the subtitles as an array.
I think is using regex.. please help.
I think this will do the trick for you:
$pattern = "|<h2>(.*)</h2>|U";
preg_match_all($pattern,$html,$matches);
foreach($matches[1] as $match)
$html = str_replace($match, "<a name='".$match."' />".$match, $html);
$array_of_elements = $matches[1];
Just make sure that $html has the existing html before this code starts. Then it will have an <a name='foo' /> added after this completes, and $array_of_elements will have the array of matching text values.

WordPress Subscribe2 plugin escapes characters in blog name when sending email

I'm using Subscribe2 plugin in my new upcoming WordPress blog (http://www.adlerr.com). My blog's title is "Roee Adler's Blog". When sending an email, Subscribe2 escapes the apostrophe in my blog's title, and the e-mail subject is received as follows:
[Roee Adler's Blog] Please confirm your request
The e-mail body is:
Roee Adler's Blog has received a request to
subscribe for this email address. To complete your
request please click on the link below:
...
I would naturally like to have the "normal" un-escaped version of my blog name in the title and body.
I asked this question on doctype.com with no success (here's the question), however from the answers I understood this probably requires changes to the PHP code of the plugin, so I would rather ask it here.
Following the answers I received on doctype, I did alter the following section of code:
function substitute($string = '') {
if ('' == $string) {
return;
}
$string = htmlspecialchars_decode(str_replace("BLOGNAME", get_option('blogname'), $string));
$string = str_replace("BLOGLINK", get_bloginfo('url'), $string);
$string = htmlspecialchars_decode(str_replace("TITLE", stripslashes($this->post_title), $string));
$string = str_replace("PERMALINK", $this->permalink, $string);
In the code above, I added a htmlspecialchars_decode wrapper for the generation of both BLOGNAME and TITLE, however the e-mail subject and body still contains '.
What can I do to resolve this?
Thanks
As per the documentation on htmlspecialchars_decode, you need to pass ENT_QUOTES as the $quote_style argument for it to convert ' to '. Try setting ENT_QUOTES:
function substitute($string = '') {
if ('' == $string) {
return;
}
$string = htmlspecialchars_decode(str_replace("BLOGNAME", get_option('blogname'), $string), ENT_QUOTES);
$string = str_replace("BLOGLINK", get_bloginfo('url'), $string);
$string = htmlspecialchars_decode(str_replace("TITLE", stripslashes($this->post_title), $string), ENT_QUOTES);
$string = str_replace("PERMALINK", $this->permalink, $string);
WordPress replaces an apostrophe in the blog title with ' before it stores it in the database. If you want to override this, edit the functions.php file and insert the following statement:
update_option("blogname", "My Blog's Title With Apostrophe");
That will force the title to be exactly what you enter. Changes to the blog title you make in the Settings menu will have no effect.

Categories