I need to automatically parse a string and find if a link to my site is present, automatically replace the address by a clickeable HTML link.
Supposing my site adresses are www.mysite.com + wap.mysite.com + m.mysite.com, I need to convert:
My pictures at m.mysite.com/user/id are great.
to:
My pictures at mysite.com/user/id are great.
The question is how to do this (with ereg_replace?) instead of using tons of lines of code.
Notice that the result must be a relative URL, so that the current protocol and subdomain is used for the target link. If the user is in the m subdomain of the HTTPS version, the target will be the m subdomain of the HTTPS protocol and so on. Only links to mysite.com must be linked, any other links must be treated as ordinary plain text. Thanks in advance!
First piece of advice, stay away from ereg, it's been deprecated for a long time. Second, you can probably google and experiment to concoct a preg expression that works well for you, so tweak what I have here to suit your needs.
I was able to put together a fairly simple regex pattern to search for the URLs.
preg_match("/m.mysite.com\S+/", $str, $matches);
Once you have the URLs, I'd suggest parse_url instead of regex.
And here is the code
$sSampleInput = 'My pictures at http://m.mysite.com/user/id are great.';
// Search for URLs but don't look for the scheme, we'll add that later
preg_match("/m.mysite.com\S+/", $sSampleInput, $aMatches);
$aResults = array();
foreach($aMatches as $sUrl) {
// Tack a scheme afront the URL
$sUrl = 'http://' . $sUrl;
// Try validating the URL, requiring host & path
if(!filter_var(
$sUrl,
FILTER_VALIDATE_URL,
FILTER_FLAG_HOST_REQUIRED|FILTER_FLAG_PATH_REQUIRED)) {
trigger_error('Invalid URL: ' . $sUrl . PHP_EOL);
continue;
} else
$aResults[] =
'<a href="' . parse_url($sUrl, PHP_URL_PATH) .
'" target="_blank">' . $sUrl . '</a>';
}
Related
I am trying to format a mailto link that works with a variety of mail clients. This works with Mac's "Mail" and Thunderbird. However, when I click the link on my Android phone, the query string in the URL is stripped starting with the equals sign. I have tried to add code to specify the content type, to parse the query string and re-add it, etc. So far, nothing works. Here's my code:
<?php
ini_set('default_charset', 'UTF-8');
$BASEURL = strtok($_SERVER["REQUEST_URI"], '?');
echo '
<li><a href="mailto:?subject=An Article Worth Reading: ' . $ALTITLE . '&body=I found this article worthwhile and think you will too:%0D%0A %0D%0A
https://worldviewpublications.org' . htmlspecialchars($_SERVER['HTTPS_HOST']) . $BASEURL . "?" . htmlspecialchars($_SERVER['QUERY_STRING']) . "%0D%0A";
echo '">share with a friend</a></li>
';
?>
I'm pretty proficient in html/css but a beginner with PHP, so I am not at all sure about the efficacy of some of the PHP I added. I'd greatly appreciate any help.
I seem to have found the solution to the problem, which I'm posting below. I've removed the code shared in my initial post that didn't help (in case you're wondering, the nonbreaking space after the colon is for Thunderbird, which does not honor the new line code).
The solution was to encode the = sign. In order to do that, I first extracted the first and last parts of the URL: (1) the first part without the query string and (2) the query string after the = sign. Then I concatenated it back together, spelling out the middle part between the main URL and the unique code at the end of the query string — the question mark (encoded as %3f), the query code identifier (IDN), and the equal sign (encoded %3d). This solution passes the entire URL, including all of the query string, to my gmail client when I'm using my Android phone.
<?php
$BASEURL = strtok($_SERVER["REQUEST_URI"], '?');
$IDEN = ltrim(($_SERVER['QUERY_STRING']), 'IDN=');
echo '
<li><a href="mailto:?subject=An Article Worth Reading: ' . $ALTITLE . '&body=%0D%0AI found this article worthwhile and think you will too:
%0D%0A%0D%0A' . htmlspecialchars("\n\r") . 'https://worldviewpublications.org' . htmlspecialchars($_SERVER['HTTPS_HOST']) . $BASEURL . '%3fIDN%3d' . $IDEN;
echo '">share with a friend</a></li>
';
?>
that might be something strange when I say that I want to encode a URL partially, But I am caught in this situation and it seems that I got no other solution...
here is my code snippet
$url = isset($_GET['u']) ? esc_url($_GET['u']) : '';
$image = isset($_GET['i']) ? $_GET['i'] : '';
maybeappend = '<a href="?ajax=photo_thickbox&i=' + encodeURIComponent(img.src) +
'&u=<?php echo urlencode($url); ?>&height=400&width=500" title=""
class="thickbox"><img src="' + img.src + '" ' + img_attr + '/></a>';
its taken from wordpress /wp-admin/press-this.php
the issue is, I cant post to my site via Press this bookmarklet
I searched the google, studied the wordpress forums and found that I need to tweek Press this button in my bookmark tool bar in broswer..
but for me, that is not a solution,,why? obviously, I cant teach every visitor to do this change in their broswer..so I have to edit my code residing on server...
how can I edit encodeURIComponent(img.src) and <?php echo urlencode($url); ?> so it DOES NOT ENCODE HTTP:// part of url,
say I got a url 'http://www.google.com I want it to be encoded as www.google.com
any suggesion?
How can I achieve my goal? might be some regex ? (dont know regex :( )
what would be the code adjustment for this??
thanks for your help..
First remove the http part using substr, then apply urlencode and then reattacht the http part.
Same method can be applied in JavaScript using the equivalent JavaScript functions.
$encoded = 'http://' . urlencode(substr($url, -7));
You could indeed use a regex to filter the http:// part of the url (which is, if I understood well, what you want to do).
Something like https?://(.*) should select the part of the url you want (the s? part takes into account potential secure links).
You'll want to make sure any matching strings are at the beginning of the strings
Javascript
Use String.replace with regex:
url.replace( /^(http|https):\/\//, '' );
^ means start of the string
PHP
Use str_replace(): php.net/str_replace
if( strpos( $url, 'http' ) == 0 ){
str_replace( array( 'http://', 'https://'), '', $url, 1 );
}
strpos can also return a FALSE so make sure you use ==.
I have some YouTube URLs stored in a database that I need to rewrite.
They are stored in this format:
$http://youtu.be/IkZuQ-aTIs0
I need to have them re-written to look like this:
$http://youtube.com/v/IkZuQ-aTIs0
These values are stored as a variable $VideoType
I'm calling the variable like this:
$<?php if ($video['VideoType']){
$echo "<a rel=\"shadowbox;width=700;height=400;player=swf\" href=\"" . $video['VideoType'] . "\">View Video</a>";
$}?>
How do I rewrite them?
Thank you for the help.
You want to use the preg_replace function:
Something like:
$oldurl = 'youtu.be/blah';
$pattern = '/youtu.be/';
$replacement = 'youtube.com/v';
$newurl = preg_replace($pattern, $replacement, $string);
You can use a regular expression to do this for you. If you have ONLY youtube URLs stored in your database, then it would be sufficient to take the part after the last slash 'IkZuQaTIs0' and place it in the src attribute after 'http://www.youtube.com/'.
For this simple solution, do something like this:
<?php
if ($video['VideoType']) {
$last_slash_position = strrpos($video['VideoType'], "/");
$youtube_url_code = substr($video['VideoType'], $last_slash_position);
echo "<a rel=\"shadowbox;width=700;height=400;player=swf\"
href=\"http://www.youtube.com/".$youtube_url_code."\">
View Video</a>";
}
?>
I cannot test it at the moment, maybe you can try to experiment with the position of the last slash occurence etc. You can also have a look at the function definitions:
http://www.php.net/manual/en/function.substr.php
http://www.php.net/manual/en/function.strrpos.php
However, be aware of the performance. Build a script which prases your database and converts every URL or stores a short and a long URL in each entry. Because regular expressions in the view are never a good idea.
UPDATE: it would be even better to store ONLY the youtube video identifier / url code in the database for every entry, so in the example's case it would be IkZuQ-aTIs0.
I have a string that contains an address to a youtube video, I want to use this to display the video in a pop-up lightbox. In the current form the link will not work in the lightbox:
http://www.youtube.com/v/CD2LRROpph0?f=videos&c=TEST&app=youtube_gdata&version=3
I has an idea to extract the video id 'CD2LRROpph0' and just append that to a regular youtube url, for example
http://www.youtube.com/watch?v=CD2LRROpph0.
Which i know works in the lightbox.
Any ideas on how to extract this code from the string???
This one will handle different protocols and different YouTube URLs (in case YouTube come out with country specific TLDs, for example).
$urlTokens = parse_url($url);
$newUrl = $urlTokens['scheme'] . '://' . $urlTokens['host'] . '/watch?v=' . preg_replace('~^/v/~', '', $urlTokens['path']);
CodePad.
$newUrl = preg_replace('#http://www.youtube.com/v/([a-z0-9_\-]{11}).*$#i',
'http://www.youtube.com/watch?v=$1', $string);
Or try this, you get an array back and can use the query you want
http://www.php.net/manual/en/function.parse-url.php
UPDATE: As it turns out, the below is caused by a caching issue on my production server. Thanks to everybody who contributed thoughtful answers.
I have a simple function on a php page that takes a url such as:
http://myurl.com/mypage.html?param1=value1
and converts it to:
http://myurl.com/searchpage.html?param1=value1
All it does it swap out the page.html portion.
To do this, I use the following:
$currentUrl = $this->getCurrentUrl(); // Grabs the current url, i.e 'http://myurl.com/mypage.html?param1=value1'
// Derive a search pattern from the current url
$pattern = "/" . str_replace(array("/", ".", "-"), array("\\/", "\\.", "\\-"), $currentUrl) . "/";
// get rid of the 'mypage.html'
$newUrl = preg_replace($pattern, 'http://myurl.com/', $currentUrl);
// replace the question mark with the correct page
$newUrl = str_replace("/?", "/searchpage.html?", $newUrl);
The above code is not the exact code but is a good representation. It works beautifully on one server, but when I push to production, the preg_replace does not work. I originally attempted to use str_replace. It also works on my local development machine, but not on the production server.
I have confirmed that the URL variables are coming in correctly. Any ideas?
That's horribly convoluted (sorry to say). Why not just:
$newUrl = preg_replace('!\bmypage\.html\b!', 'searchpage.html', $oldUrl);
Why don't you just do
$pieces = explode('/',$url);
str_replace('mypage','searchpage',$pieces[2]);
$newURL = implode('/',$pieces);
Way better than using regexps.