Regex to capture <img> src in php [duplicate] - php

This question already has answers here:
How to extract img src, title and alt from html using php? [duplicate]
(10 answers)
Closed 8 years ago.
I'd like to extract the img src and using preg_match_all I have this:
$tag = '<img src="path/to/image.png" />';
preg_match_all('/(width|height|src)=("[^"]*")/i',$tag, $img[$tag]);
which returns:
Array
(
[<img src="path/to/image.png" />] => Array
(
[0] => Array
(
[0] => src="path/to/image.png"
)
[1] => Array
(
[0] => src
)
[2] => Array
(
[0] => "path/to/image.png"
)
)
)
How can I write the regex to return a similar result regardless of double or single quotes used in tag? I can write:
$tag = "<img src='path/to/image.png' />";
preg_match_all('/(width|height|src)=(\'[^\']*\')/i',$tag, $img[$tag]);
Which works, but I'm not familiar enough with regex to write one expression to handle either. I did try:
preg_match_all('/(width|height|src)=((\'[^\']*\')|("[^"]*"))/i',$tag, $img[$tag]);
But this seems to return extra matches in the array which I don't want.

You can use this:
(width|height|src)=("[^"]*"|'[^']*')
I've basically used an alternation to either match "fds" or 'fds'.

Related

preg match the absolute first and last tags [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 2 months ago.
I have this code:
preg_match_all('/<script>(.*)<\/script>/', $matches[0], $page_script);
Page html:
<script>some_script_data</script>
<script>some_more_script_data</script>
What I expected is 2 results like:
[
0 => "<script>some_script_data</script>",
1 => "<script>some_more_script_data</script>"
]
What I get is :
[
0 => "<script>some_script_data</script><script>some_more_script_data</script>"
]
So it is taking everything between the very fist <script> and the very </script>. How can I solve this ?
You are close, but need to access the result slightly different. Take a look at that:
<?php
$markup = <<<HTML
<script>some_script_data</script>
<script>some_more_script_data</script>
HTML;
preg_match_all('/<script>(.*)<\/script>/', $markup, $scripts);
print_r($scripts);
The output is:
Array
(
[0] => Array
(
[0] => <script>some_script_data</script>
[1] => <script>some_more_script_data</script>
)
[1] => Array
(
[0] => some_script_data
[1] => some_more_script_data
)
)

Php preg_match_all does not behave as expected [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
Trying to capture all text between tags.
Code:
$test = '<test>foo<tests> asdlkfjklas lkflsdakj <test>sdfsd<tests> asdlkaskl <test>235234<tests>';
$match = '/<test>(.*)<tests>/';
preg_match_all($match, $test, $nextLink);
Result of print_r:
Array ( [0] => Array ( [0] => foo asdlkfjklas lkflsdakj sdfsd asdlkaskl 235234 ) [1] => Array ( [0] => foo asdlkfjklas lkflsdakj sdfsd asdlkaskl 235234 ) )
your regex syntax is greedy. use folowing:
$match = '/<test>(.*?)<tests>/';

Need to extract words from string in php [duplicate]

This question already has answers here:
Extract words from string with preg_match_all
(7 answers)
Closed 8 years ago.
I need to extract the names from the following string:
$contact = "John96783819Dickson97863424"
i tried using this:
preg_match('/[a-zA-Z]/',$contact,$matches);
but i get an array with all the alphabets individually in the array.
Desired Output:
Array ([0] => 'John', [2] => 'Dickson')
And now it gets complicated. The same reggae should extract this
$contact = 'Vincent Tan96123179Lawrence Thoo90603123Ryan Ong91235721'
into this
Array ([0] => 'Vincent Tan', [2] => 'Lawrance Thoo' , [3] => 'Ryan Ong')
How do i do that?
All you need is to quantify the character class using +
/[a-zA-Z]+/
+ matches one occurence of presceding regex
Example : http://regex101.com/r/bI6aH1/1
preg_match_all('/[a-zA-Z]+/',$contact,$matches);
Will give output as
Array ( [0] => John [1] => Dickson )
preg_match('/[a-zA-Z]+/',$contact,$matches);
The /[a-zA-Z]/ means match any ONE letter, anywhere in the string. Adding the + in /[a-zA-Z]+/ means match one or MORE sequential letters.

PHP preg_match_all syntax [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I'm using preg_match_all to import images using the
Currently works:
//get image url from url
preg_match_all('/<img[^>]+>/i',$file->body, $images);
I tried this:
//get image url from url
preg_match_all('/<img id="charity"[^>]+>/i',$file->body, $images);
Once altered I get a 500 server error so I assume my syntax is wrong. How do I correctly alter this to work correctly?
That regexp seems to be OK. Your error must be anywere else.
You can test it with something like:
<?php
$test='
<img src="http://example.com/image.jpg"/>
foo bar
<img id="charity" src="local/image.jpg"/>
';
preg_match_all('/<img[^>]+>/i',$test, $images);
print_r($images);
preg_match_all('/<img id="charity"[^>]+>/i',$test, $images);
print_r($images);
Output:
Array
(
[0] => Array
(
[0] => <img src="http://example.com/image.jpg"/>
[1] => <img id="charity" src="local/image.jpg"/>
)
)
Array
(
[0] => Array
(
[0] => <img id="charity" src="local/image.jpg"/>
)
)
Tested with PHP 5.2.13.

Regular expression in PHP to return array with all images from html, eg: all src="images/header.jpg" instances

I'd like to be able to return an array with a list of all images (src="" values) from html
[0] = "images/header.jpg"
[1] = "images/person.jpg"
is there a regular expression that can do this?
Many thanks in advance!
Welcome to the world of the millionth "how to exactract these values using regex" question ;-) I suggest to use the search tool before seeking an answer -- here is just a handful of topics that provide code to do exactly what you need;
replacing all image src tags in HTML text
getting image src in php
How to extract img src, title and alt from html using php?
Matching SRC attribute of IMG tag using preg_match
php regex : get src value
Dynamically replace the “src” attributes of all <img> tags (redux)
preg_match_all , get all img tag that include a string
/src="([^"]+)"/
The image will be in group 1.
Example:
preg_match_all('/src="([^"]+)"/', '<img src="lol"><img src="wat">', $arr, PREG_PATTERN_ORDER);
Returns:
Array
(
[0] => Array
(
[0] => src="lol"
[1] => src="wat"
)
[1] => Array
(
[0] => lol
[1] => wat
)
)
Here is a more polished version of the regular expression provided by Håvard:
/(?<=src=")[^"]+(?=")/
This expression uses Lookahead & Lookbehind Assertions to get only what you want.
$str = '<img src="/img/001.jpg"><img src="/img/002.jpg">';
preg_match_all('/(?<=src=")[^"]+(?=")/', $str, $srcs, PREG_PATTERN_ORDER);
print_r($srcs);
The output will look like the following:
Array
(
[0] => Array
(
[0] => /img/001.jpg
[1] => /img/002.jpg
)
)
I see that many peoples struggle with Håvard's post and <script> issue. Here is same solution on more strict way:
<img.*?src="([^"]+)".*?>
Example:
preg_match_all('/<img.*?src="([^"]+)".*?>/', '<img src="lol"><img src="wat">', $arr, PREG_PATTERN_ORDER);
Returns:
Array
(
[1] => Array
(
[0] => "lol"
[1] => "wat"
)
)
This will avoid other tags to be matched. HERE is example.

Categories