PHP url create from string - php

I would like to check a string and convert all the substrings that could be potential links inside the original string like http://www.google.com, or www.google.com, replaced with
<a href='http://www.google.com'>http://www.google.com</a> so that i can create real links from them.
How can i do this?

you can create the HTML links by calling the following function in PHP:
$stringToCheck = 'http://www.google.com, or www.google.com';
$stringWithHTMLLinks = '';
$stringWithHTMLLinks = preg_replace('/\b((https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&##\/%?=~_|!:,.;]*[A-Z0-9+&##\/%=~_|]/si', '\0', $stringToCheck);

Use this regex provided on Daring Fireball to match an URL.

Related

how to filter link url http:// and www with preg replace

i made some function using preg_replace. the code is to get the preview of it.
i made this code
$strings = htmlspecialchars_uni($thread['soc_instagram']);
$searchs = array('~(?:https://instagram\.com/p/)?([a-zA-Z0-9_\-+?:]+)~');
$replaces = array('https://instagram.com/p/$1/media/?size=l');
$soc_instagram = preg_replace($searchs,$replaces,$strings);
the code work perfect if i post an instagram with this url https://instagram.com/p/BarUcqwht_u
and it will produce code
https://instagram.com/p/BaQsAubg6H3/media/?size=l
but the problem is when i try to add WWW in the url, something like this https://www.instagram.com/p/BarUcqwht_u the code will produce error string
the result will be like this
https://instagram.com/p/https:/media/?size=l//https://instagram.com/p/www/media/?size=l.https://instagram.com/p/instagram/media/?size=l.https://instagram.com/p/com/media/?size=l/https://instagram.com/p/p/media/?size=l/https://instagram.com/p/BarUcqwht_u/media/?size=l/
i try to add WWW in my preg_replace code but the result will be like this
https://www.instagram.com/p/https:/media/?size=l//https://www.instagram.com/p/instagram/media/?size=l.https://www.instagram.com/p/com/media/?size=l/https://www.instagram.com/p/p/media/?size=l/https://www.instagram.com/p/BaQsAubg6H3/media/?size=l
any help will be nice, thanks
Add the www. after the protocol and make it optional. preg_replace also doesn't require arrays, strings work fine.
$strings = 'https://www.instagram.com/p/BarUcqwht_u';
$searchs = '~(?:https://(?:www\.)?instagram\.com/p/)?([a-zA-Z0-9_\-+?:]+)~';
$replaces = 'https://instagram.com/p/$1/media/?size=l';
$soc_instagram = preg_replace($searchs,$replaces,$strings);
echo $soc_instagram;
Demo: https://3v4l.org/WSors
What your current implementation does is replaces characters not listed in your character class with the URL. See https://regex101.com/r/PXb2h1/2/ for a visual representation.

Regex to filter int from url

I'm trying to filter out a value of a url.
The url looks like the following:
http&colon;//userimages-akm.imvu.com/catalog/includes/modules/phpbb2/images/avatars/145870556_47076915459092eafd7b69.jpg
Now i'm trying to only receive the following part from the url: 145870556
I thought about using a regex. But i won't get a working regex beside this one:
^((http[s]?|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$
Is there a better regex to use?
If the image filename always follows the same format <timestamp>_<hex-value>.<extension>, then you don't need to match the entire URL.
$url = 'http://userimages-akm.imvu.com/catalog/includes/modules/phpbb2/images/avatars/145870556_47076915459092eafd7b69.jpg';
preg_match_all('~\/(\d+)_.*$~', $url, $matches);
// $matches[1] = '145870556';
https://regex101.com/r/vsDnoj/2

How can I convert the non url friendly characters to URL friendly characters in address bar?

How can I convert the non-url friendly chracters in address bar to show it as url-friendly slug.
For example I want the string http://www.example.com/?q=test%20words%20string to be displayed as http://www.example.com/?q=test words string or http://www.example.com/?q=test-words-string.
P.S.: I am passing the value using GET method in form.
You can replce string. Replace the space with -
<?php $your_string = str_replace(' ', '-', strtolower($your_string)); ?>
You need the urldecode function:
$str = urldecode($url);
You may then convert the string $str any way you wish, for example use a sluggifier, which is probably the best option for all cases. It takes a lot of things into account you probably won't be able to do yourself. This is an example of a sluggifier.
If you use Symfony you have the sluggifier as a plug-in, so it also depends on what framework you are using, if you use one at all.
You need to first decode the url
$url = "http://www.example.com/?q=test%20words%20string";
$nUrl = urldecode($url) //outputs http://www.example.com/?q=test words string
now
$fUrl = str_replace(' ','-',$nUrl);

about use regex to convert url to link

I need to convert the url in the article to the 3g domain.
for example, i need to convert
here is the link:http://www.mydomain.com/index thanks
to
here is the link:<a href='http://3g.mydomain.com$4' target='_self'>http://3g.$3.com$4</a> thanks
don't convert the other domain, just mydomain. here is the code:
$c = "/([^'\"=])?http:\/\/([^ ]+?)(mydomain)\.com([A-Za-z0-9&%\?=\/\-\._#]*)/";
$b=preg_replace($c, "$1<a href='http://3g.$3.com$4' target='_self'>http://3g.$3.com$4</a>",$b);
it works very well,but if the text like this:
a link
it will return the wrong result like this:
a link
but l need the result of
a link
how should i do?
You should do the following:
Strip target attributes from existing hyperlinks
Rewrite hyperlinks in href attributes
Rewrite any other hyperlinks
$plain = "http://([^ ]+?)(mydomain)\.com(/?[^'\"\s]*(?=['\"\s]))";
$plain_replace = "http://3g.$3.com$4";
$in_href = "href=(['\"])" + plain + "(['\"])";
$in_href_replace = "href='http://3g.$3.com$4' target='self'";
$strip_target = "target=['\"][^'\"]*['\"]";
...
So:
Replace $strip_target with ""
Replace $in_href with $in_href_replace
Replace $plain with $plain_replace
(The regexes are tested to work in C#, you might have to adjust the \ escaping to suit the php regex rules.)
Get rid of the first ? in your regular expression. That allows for the absence of a preceding character.
Or, perhaps more to your intention, if you want to allow URLs at the beginning, you can replace:
([^'\"=])?
with:
(^|[^'\"=])
...which will allow a link if at the very beginning, or if not preceded by a quote, etc., but not otherwise.

Replace one URL with another

In PHP, replace one URL with another within a string e.g.
New post on the site http://stackoverflow.com/xyz1</p>
becomes:
New post on the site http://yahoo.com/abc1</p>
Must work for repeating strings as above. Appreciate this is simple but struggling!
function replace_url($text, $newurl) {
$text = preg_replace('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $newurl, $text);
return $text;
}
Should work.
Regex stolen from here. This will replace all URLs in the string with the new one.
Use str_replace():
$text = str_replace('http://stackoverflow.com/xyz1', 'http://yahoo.com/abc1', $text);
That will replace the first URL with the second URL in $text.
Try this:
preg_replace('#(https?://)(www\.)?stackoverflow.com\b#', '\1\2yahoo.com', $text);
If you want to change the path after the url, add another group and use preg_replace_callabck. More information in the PHP documentation.

Categories