Php: Regular Expression to Get a Url from a String

PHP regex extract url with pattern from string

You can repeat all the allowed characters before and after matching /products/ using the same optional character class. As the character class is quite long, you could shorten the notation by wrapping it in a capture group and recurse the first subpattern as (?1)

Note that you don't have to escape the forward slash using a different separator.

$re = '`\b(?:(?:https?|ftp)://|www\.)([-a-z0-9+&@#/%?=~_|!:,.;]*)/products/(?1)[-a-z0-9+&@#/%=~_|]`';

$str = <<<EOF
http://example.com/products/1/abc
This string is valid - http://example.com/products/1
This string is not valid - http://example.com/order/1
EOF;

preg_match_all($re, $str, $matches);
print_r($matches[0]);

Output

Array
(
[0] => http://example.com/products/1/abc
[1] => http://example.com/products/1
)

php regex get first URL from string

According to the documentation:

preg_match_all — Perform a global regular expression match

Since you are after just one, you should be using preg_match:

Perform a regular expression match

$regex = '/https?\:\/\/[^\" ]+/i';
$string = "lorem ipsum http://google.com lorem ipusm dolor http://yahoo.com/something";
preg_match($regex, $string, $matches);
echo $matches[0];

Yields:

http://google.com

Extract URL's from a string using PHP

REGEX is the answer for your problem. Taking the Answer of Object Manipulator.. all it's missing is to exclude "commas", so you can try this code that excludes them and gives 3 separated URL's as output:

$string = "The text you want to filter goes here. http://google.com, https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/";

preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $string, $match);

echo "<pre>";
print_r($match[0]);
echo "</pre>";

and the output is

Array
(
[0] => http://google.com
[1] => https://www.youtube.com/watch?v=K_m7NEDMrV0
[2] => https://instagram.com/hellow/
)

regex for extracting all urls from string

This should get you started:

\b(?:https?://)?(?:(?i:[a-z]+\.)+)[^\s,]+\b


Broken down, this says:

\b                   # a word boundary
(?:https?://)? # http:// or https://, optional
(?:(?i:[a-z]+\.)+) # any subdomain before
[^\s,]+ # neither whitespace nor comma
\b # another word boundary

See a demo on regex101.com.

regex to fetch url from string

This regex is what you're looking for (mandatory regex101 link):

(https?:\/\/\S+)|([a-z]+\.[a-z]+(?:\/\S+)?)

It's basically the two regexes https?:\/\/\S+ and [a-z]+\.[a-z]+(?:\/\S+)? placed into capturing groups (so that you can extract all URLs with a global search) and then combined with an OR.

https?:\/\/\S+ finds URLs that are prefixed with http:// or https:// by matching:

  • The string "http" literally http, followed by
  • An optional "s" s? followed by
  • A colon and two forward slashes :\/\/, followed by
  • One or more non-whitespace characters \S+

If https?:\/\/\S+ doesn't match, then [a-z]+\.[a-z]+(?:\/\S+)? kicks in and finds URLs that are not prefixed with http:// or https:// and whose top level domains don't contain numbers by matching:

  • One or more lowercase letters [a-z]+, followed by
  • A dot \., followed by
  • One or more lowercase letters [a-z]+, followed by
  • An optional group, which consists of

    • A forward slash \/, followed by
    • One or more non-whitespace characters \S+

Find url from string with php

A working method for linking with http/https/ftp/ftps/scp/scps:
$newStr = preg_replace('!(http|ftp|scp)(s)?:\/\/[a-zA-Z0-9.?&_/]+!', "<a href=\"\\0\">\\0</a>",$str);

I strongly advise NOT linking when it only has a dot, because it will consider PHP 5.2, ASP.NET, etc. links, which is hardly acceptable.

Update: if you want www. strings as well, take a look at this.

PHP get image url from html-string using regular expression

Use preg_match_all() and rearrange your result:

<?php
$html = <<<EOT
<div style="background-image : url(https://exampel.com/media/logo.svg);"></div>
<img src="https://exampel.com/media/my-photo.jpg" />
<div style="background-image:url('https://exampel.com/media/icon.png');"></div>
EOT;

preg_match_all(
'/<img.+src=[\'"](.+?)[\'"].*>|background-image ?: ?url\([\'" ]?(.*?\.(?:png|jpg|jpeg|gif|svg))/i',
$html,
$matches,
PREG_SET_ORDER
);

$image = [];
foreach ($matches as $set) {
unset($set[0]);
foreach ($set as $url) {
if ($url) {
$image[] = $url;
}
}
}

echo '<pre>' . print_r($image, true) . '</pre>' . PHP_EOL;


Related Topics



Leave a reply



Submit