php regex get first URL from string

Related searches

I'm using the following regex in PHP to grab the URLs from a string

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

$urls returns all the URLs. How can return only the first URL? In this case http://google.com.

I'm trying to achieve this without using a foreach loop.

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

parse_url - Manual, thomas at gielfeldt dot com ¶. 8 years ago. [If you haven't yet] been able to find a simple conversion back to string from a parsed url, here's an example: <?php This is a short guide on how to get the first character of a string in PHP. As an added bonus, I will also show you how to get the first two characters of a string. We used two different approaches here. Using the first approach, we treat the string like an array. i.e. We use the index position of the character to access it.

Use preg_match instead of preg_match_all

preg_match - Manual, preg_match_all — Perform a global regular expression match $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on. Get number from string regex I am trying to get the enclosed number between two slashes in a URL using regex. The code regex I have is not working, I am fairly new to regex and don't really understand it.

preg_match_all() has a flag parameter which you can use to order the results. The parameter in which you have the variable $matches is your results and should be listed in that array.

$matches[0][0];//is your first item.
$matches[0][1];//is your second

It would be better to use preg_match() rather than preg_match_all().

Here's the documentation on preg_match_all() for your flags. Link here!

preg_match_all - Manual, The first approach is used to find the sub string matching in a string and second approach is to find a regular expression match. PHP contains functions for these​  Find the Image URL using preg_match() syntax or something similar to extract JPG or PNG or GIF URLs from a mixed text and put them in an array or at last store the first url.

^.*?(https?\:\/\/[^\" ]+)

Try this.Grab the capture or group.See demo.

https://regex101.com/r/pM9yO9/5

$re = "/^.*?(https?\\:\\/\\/[^\\\" ]+)/";
$str = "lorem ipsum http://google.com lorem ipusm dolor http://yahoo.com/something";

preg_match_all($re, $str, $matches);

How to check if URL contain certain string using PHP , Tutorial on using PHP to parse HTML files and extract links using pcre regex not including, the next double-quote (") - 1st capture; the string: ">; a series of $​url = "http://www.example.net/somepage.html"; $input = @file_get_contents($url)​  PHP Regex for Web Developers. Regular expressions are a very useful tool for developers. They allow to find, identify or replace a word, character or any kind of string. This tutorial will teach you how to master PHP regexp and show you extremely useful, ready-to-use PHP regular expressions that any web developer should have in his toolkit.

Just print the 0th index.

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

Output:

http://google.com

Parsing HTML to find Links < PHP, But if the START parm is equal to the LENGTH of the string, then you'll get an empty string (PHP 7) or FALSE (prior to PHP 7). An example of this would be 'substr ("x", 1, 1)' , which would return either an empty string or FALSE. Because your "x", which has a LENGTH of '1', is in the 0th position,

As I guessed, strpos() is always faster (about 2x) for short strings like a URL but for very long strings of several paragraphs (e.g. a block of XML) when the string doesn't start with the needle preg_match as twice as fast as strpos() as it doesn't scan the entire string.

Extracting URL’s from a string of HTML text using the php preg_match_all() function can grab you an array of the full URLs or an array of just the root domains depending on the RegEx used. I’ll provide a premade function snippet that you can use for both. The only difference between these two functions is the RegEx used.

The eight regular expressions we'll be going over today will allow you to match a (n): username, password, email, hex value (like #fff or #000), slug, URL, IP address, and an HTML tag. As the list goes down, the regular expressions get more and more confusing. The pictures for each regex in the beginning are easy to follow, but the last four

Comments
  • possible duplicate of Finding urls from text string via php and regex?
  • @LalitSharma Not a duplicate
  • @CyberJunkie an improved version would be '~https?://[^" ]+~i'