I am using following code for convert url to hyperlink on text.But the problem is i want to use shorten title for hyperlink for example this is url http://stackoverflow.com/questions/ask?title=convert%20url%20to%20hyperlink%20on%20text%20as%20formatted and after convert like this :
http://stackoverflow.com/questions/ask?title=convert%20url%20to%20hyperlink%20on%20text%20as%20formatted
I want to this :
http://stackoverflow.com/...
This is my code :
$stringdata = preg_replace('|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '$1 $2', $stringdata);
Title should be shorten but url should be same of original.
Thankyou.
You could always use parse_url found here
Here's an example:
$url = 'http://www.stackoverflow.com/questions/ask?title=convert%20url%20to%20hyperlink%20on%20text%20as%20formatted';
$splitUrl = parse_url($url);
echo '<a href="' . $url . '"/>' . $splitUrl['scheme'] . '://' . $splitUrl['host'] .'/... </a>';
parse_urlcreates an array from the URL provided.
UPDATE:
Using Mikael Roos answer at that link I came up with what you needed it to do.
function make_clickable($text) {
$regex = '#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#';
return preg_replace_callback($regex, function ($matches) {
$splitUrl = parse_url($matches[0]);
return "<a href='{$matches[0]}'>{$splitUrl['scheme']}://{$splitUrl['host']}/..</a>";
}, $text);
}
echo make_clickable('Some odd text here that makes https://stackoverflow.com/questions/ask?title=convert%20url%20to%20hyperlink%20on%20text%20as%20formatted clickable');
try this
$var='http://stackoverflow.com/questions/ask?title=convert%20url%20to%20hyperlink%20on%20text%20as%20formatted';
$array=explode('/', $var);
$url=$array[0]."//".$array[2]."/...";
Related
This is how I get my url on my localhost:
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
echo $url;
it returns : http://localhost/CodeSensei/menu because I am on the menu page.
How can I trim this? I only want "http://localhost/CodeSensei"
I know I can trim it like this
echo trim($url,"menu");
But the problem is, that "menu" is dynamic, it keep changing dependes on the page. Is there any way to trim my url so it will always and only print "http://localhost/CodeSensei" in any page?
There are many ways to achieve this. You can play around with different string manipulators like explode() etc.
Here is a solution using explode()
$variable = "http://localhost/CodeSensei/menu";
$variable = (explode("/",$variable));
$url='';
for($i=2;$i<count($variable)-1;$i++)
{
$url .= "/".$variable[$i];
}
$final_url = "http:/".$url;
echo $final_url;
Your output
http://localhost/CodeSensei
This function may help you. It get a url and return the url without after the last /.
<?php
function getSubUrl($originUrl) {
$url = parse_url($originUrl);
$url['scheme'] .= '://';
$url['path'] = dirname($url['path']);
return implode($url);
}
echo getSubUrl('http://localhost/CodeSensei/menu/123') . PHP_EOL;
// http://localhost/CodeSensei/menu
echo getSubUrl('http://localhost/CodeSensei/menu') . PHP_EOL;
// http://localhost/CodeSensei
echo getSubUrl('http://localhost/CodeSensei') . PHP_EOL;
// http://localhost/
echo getSubUrl('http://localhost/') . PHP_EOL;
// http://localhost/
echo getSubUrl('http://localhost') . PHP_EOL;
// http://localhost
What I want
If the URL in the string contains a .jpg at the end of the URL (not the string) then it should make an image from it with preg_replace else make a normal link.
so for example:
If I have http://www.example.com/images/photo.jpg then it should replace with:
<img src="http://www.example.com/images/photo.jpg" alt="http://www.example.com/images/photo.jpg">
The problem:
The URL is replaced with a link in any way and my regex isn't working :( .
What I have tried:
$content = preg_replace("/(http:\/\/[^\s]+(?=\.jpg))/i","<img src=\"$1\" alt = \"$1\"></img>",$content);
$content = nl2br(preg_replace("/(http:\/\/[^\s]+(?!\.jpg))/m", "$1", $content));
Try this
function replace_links($content)
{
if (preg_match('#(http://[^\s]+(?=\.(jpe?g|png|gif)))#i', $content))
{
$content = preg_replace('#(http://[^\s]+(?=\.(jpe?g|png|gif)))(\.(jpe?g|png|gif))#i', '<img src="$1.$2" alt="$1.$2" />', $content);
}
else
{
$content = preg_replace('#(http://[^\s]+(?!\.(jpe?g|png|gif)))#i', '$1', $content);
}
return $content;
}
$content = preg_replace('#\b(http://\S+\.jpg)\b#i', '<img src="$1" alt="$1" />', $content);
You don't need lookaround. Just go with
$content = preg_replace("#(http://[^ ]+\\.jpg(?= |$)#i","<img src=\"$1\" alt=\"$1\"/>", $content);
I think you used the lookahead operator when you wanted lookbehind. You could change (?=\.jpg) to (?<=\.jpg) but there are other, cleaner regex's I'm sure others will post.
This worked for me.
$parse_img='Hello, http://orbitco-ccna-pastquestions.com/images/Q5.jpg
In the figure above, router R1 has two point-to-point . ';
$parse_img=preg_replace('/(https?:\/\/(.\*)?\\.jpg|png|gif)[\s+]*/i',"< img src=\"$1\" alt = \"$1\">< /img >",$parse_img);
echo $parse_img;
Suyash
I need to extract the link value which is stored in a <a> tag by using php code.
I used below code
$url = '<a title="Question" href="http://stackoverflow.com/questions/ask">t</a>';
preg_match("/href=\"(.*?)\"/i", $url, $matches);
print_r($matches);
With the above code I can able to get the value of a single href. But it is not working for multiple href in a string(i.e $url = '<a title="Question" href="http://stackoverflow.com/questions/ask">t</a>x';). How can I achieve this?
Use a DOM parser, this example should get you going:
<?php
$doc = new DOMDocument();
$doc->loadHTML('<a title="Question" href="http://stackoverflow.com/questions/ask">t</a>');
$elm = $doc->getElementsByTagName('a')->item(0);
foreach ($elm->attributes as $attr) {
echo $attr->name . ' ' . $attr->value . '<br>';
}
echo "Directly getting href: " . $elm->attributes->getNamedItem('href')->value;
Output:
title Question
href http://stackoverflow.com/questions/ask
Directly getting href: http://stackoverflow.com/questions/ask
Demo: http://viper-7.com/EN1Usi
Docs: http://php.net/manual/en/class.domdocument.php
Use preg_match_all() to get all the matches, not just the first.
$url = '<a title="Question" href="http://stackoverflow.com/questions/ask">t</a> x';
preg_match_all("/href=\"(.*?)\"/i", $url, $matches);
print_r($matches);
The following code comes in from the database and the mw and mh values are messing everything up. How can I strip those out?
Here is my code:
<a class="wgt_ListingsSpread_thumblink" href="/listings/1193975-19-xxxx--whitby-ontario">
<img border="0" src="http://media.realwebleads.com/mlsphoto.php?mls=toronto&lid=1193975&pic=0&mw=160&mh=138">
You could use this function to remove parameters from a give URL:
function removeParamter($url, $varname) {
list($urlpart, $qspart) = array_pad(explode('?', $url), 2, '');
parse_str($qspart, $qsvars);
#unset($qsvars[$varname]);
$newqs = http_build_query($qsvars);
return $urlpart . '?' . $newqs;
}
In your case you would need to call it twice to remove the 2 paramters mw and mh:
$url = 'http://media.realwebleads.com/mlsphoto.php?mls=toronto&lid=1193975&pic=0&mw=160&mh=138'
$url = removeParamter($url, 'mw');
$url = removeParamter($url, 'mh');
//wanted URL
echo $url;
i made a function for making a keywords from the post title and replace each word in the full post.
this is my function
function myseonew($title,$text){
$title = stripslashes($title);
$text = stripslashes($text);
$fburl = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$keywords = explode(" ",$title);
$regex = '/('.implode('|', $keywords).')/i';
$output = preg_replace($regex, '<a id="smalltext" href="'.$fburl.'">\\1</a>', $text);
return $output;
}
but i faced a problem for the output the characters all of it comes ( �������� ).
so is there any way to solve this issue
by the way my encoding is UTF-8
regards
try using u modifier in your regexp:
$regex = '/('.implode('|', $keywords).')/iu';