Php preg_match_all does not behave as expected [duplicate] - php

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>/';

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
)
)

newbie: php preg_match_all not working as expected [duplicate]

This question already has answers here:
What do 'lazy' and 'greedy' mean in the context of regular expressions?
(13 answers)
Closed 8 years ago.
i'm trying to parse tags from a string like the following:
$string = "foo [cmd:tag1] bar [cmd:tag2] bla bla";
$pattern = "/\[cmd:(.+)\]/";
preg_match_all($pattern, $string, $matches, PREG_OFFSET_CAPTURE);
$rc = $matches[0];
foreach($rc as $tag)
{
print_r2($tag);
}
which will return:
Array
(
[0] => [cmd:tag1] bar [cmd:tag2]
[1] => 4
)
what is wrong in my syntax as i'm expecting the following result:
Array
(
[0] => [cmd:tag1]
[1] => [cmd:tag2]
)
thanks
\[cmd:(.+?)\]
or use
\[cmd:([^\]]*)\]
Make your quantifier * non greedy by putting ? ahead of it.
See demo.
https://regex101.com/r/fA6wE2/23

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

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'.

How can I make preg_match_all array [duplicate]

This question already has answers here:
Preg_match_all returning array within array?
(2 answers)
Closed 8 years ago.
I have this:
$text = $_POST['text'];
When I echo $text I get this:
ggg #hhh #ddd ggg hhhrr ggg #ttt
When I do this:
$val = preg_match_all("/#\w+/", $text, $matches);
print_r($matches);
I get
Array ( [0] => Array ( [0] => #hhh [1] => #ddd [2] => #ttt ) )
But I want output like this:
Array ( [0] => #hhh [1] => #ddd [2] => #ttt )
thanks
Another approach is to use named groups.
$val = preg_match_all("/(?P<myLabel>#\w+)/", $text, $matches);
print_r($matches['myLabel']);
Take the first (zeroth) item from the array by changing this:
print_r($matches);
To this:
print_r($matches[0]);

string to array with desired keys [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert a string into list of arrays
I have a string code=AA&price=10&user_id=5.initially i exploded first with & and then by = but i getting.
array(
[0] =>code
[0] =>AA
[0] =>price
[0] =>10
[0] =>user_id
[0] =>5
)
My aim is to show
array(
[code] => AA
[price] => 10
[user_id] => 5
)
parse_str($str, $values);
var_dump($values);
You are parsing a URL encoded format, use the already existing parse_str to parse it.
$string = 'code=AA&price=10&user_id=5';
$params = array();
parse_str($string, $params);
print_r($params);
http://php.net/manual/en/function.parse-str.php
parse_str — Parses the string into variables
parse_str('code=AA&price=10&user_id=5', $outputArray);

Categories