PHP preg_match an html arttribue with number seprated by a comma - php

I'm trying to get the values of an attribute and fill them in an array:
<p numbers="11.2,1.1 2,3,3.1 33"></p>
preg_match('/numbers="([0-9\\.]]+)"/',$elements,$match);
I need something like this:
$match[0] -> 11.2
$match[1] -> 1.1
$match[2] -> 2
Or anything that would get me to store the numbers in an array, but I can't figure out the regex for it.

Personally I would simply match all numbers and then split them
$matches = preg_match('/numbers="(.*)"/',$elements,$match);
$numbers = explode(',', $matches[1]);

I think I got one solution:
$string = '<tag> etc</tag>
<p numbers="11.2,1.1 2,3,3.1 33"></p>
<div> etc</div>';
// Catch only the numerals from the attribute numbers
preg_match('/<p.*numbers="(.*)"/', $string, $match);
// Catch each number separated by comma or space.
preg_match_all('/(\d+(\.?|\s?)(\d+)?)/', $match[0], $matches);
echo '<pre>';
print_r($matches[0]);
The regex is somehow pretty straightforward, but if you need some clarification, write a comment.
And you can see it tested here: https://3v4l.org/iVp1v

Related

How to strip and get the code between two lines in PHP?

I'm trying to assign it to a variable in PHP, but all the Regex and preg_replace I've tried doesn't help me. Here is a sample text.
Claim Code:
7241B-2HWRXR9-2P2BA
$1.00
I want to pull out exactly what is in the middle, which is 7241B-2HWRXR9-2P2BA.
You can use the following to match:
Claim Code:\s*([\w-]+)\s*\$(\d+(?:\.\d+)?)
And you can pull out whatt you want by $1
See DEMO
If the code has always the same format (5-7-5 chars), you can use:
$str = 'Claim Code:
7241B-2HWRXR9-2P2BA
$1.00';
preg_match('~[\w]{5}\-[\w]{7}\-[\w]{5}~', $str, $matches);
echo $matches[0]; // returns 7241B-2HWRXR9-2P2BA
UPDATE
For optional code length this regex is possible:
preg_match('~.*:\s+([\w\-]{10,25})\s+.*~', $str, $matches);
echo $matches[1]; // ^^ here set the min and max code length, or remove it
// without setting min/max code length:
preg_match('~.*:\s+([\w\-]+)\s+.*~', $str, $matches);
Less elegant than a regexp / preg_replace, but this should work, too: Put the string into an array, then only use line 2 (array element number 1).
<?php
$string = 'Claim Code: ...............';
$lines = explode("\n", $string); //Transform the string into an array, separated by new lines (\n) (each index in the array is a single line from the string
echo $lines[1]; //this is 2nd line of the string, i.e. the claim code

regex to match string with numbers

I have a query string stored in a variable and I need to strip out some stuff from it using preg_replace()
the parameters I want to strip out look like this:
&filtered_features[48][]=491
As there will be multiples of these parameters in the query string the 48 and the 491 can be any number so the regex needs to essentially match this:
'&filtered_features[' + Any number + '][]=' + Any number
Anyone know how I would do this?
$string = '&filtered_features[48][]=491';
$string = preg_replace('/\[\d+\]\[\]=\d+/', '[][]=', $string);
echo $string;
I assume you wanted to remove the numbers from the string. This will match a multi-variable query string as well since it just looks for [A_NUMBER][]=A_NUMBER and changes it to [][]=
$query_string = "&filtered_features[48][]=491&filtered_features[49][]=492";
$lines = explode("&", $query_string);
$pattern = "/filtered_features\[([0-9]*)\]\[\]=([0-9]*)/";
foreach($lines as $line)
{
preg_match($pattern, $line, $m);
var_dump($m);
}
/\&filtered_features\[(?<n1>\d*)\]\[\]\=(?<n2>\d*)/'
this will match first number in n1 and second in n2
preg_match_all( '/\&filtered_features\[(?<n1>\d*)\]\[\]\=(?<n2>\d*)/', $str, $matches);
cryptic answer will replace more than necessary with this string:
&something[1][]=123&filtered_features[48][]=491

Replace paragraph tag using regex

I need to take the output of $one and make an array, however, the regex is not right because all I get back is an array with one key and one value; basically, the entire string. If there are 10 paragraph tags in the string then I should get 10 values in the array.
What is wrong with my regex?
Ideally, what I would like to have in the output array are two arrays, one with the paragraph tags and another with just the text between them.
$one = preg_replace( '/<p>/i', '<p class="test">', $str ); // This gives me what I need
print_r(explode( '/<p class="test">/iU', $one )); // This does not
The problem is simple. Explode does not use regex. This should work for you.
print_r(explode('<p class="test">', $one ));
EDIT: This ought to do what you want.
$pattern = '/(?P<open><p class="test">)'
. '(?P<content>.*)'
. '(?P<close><\/p>)/i';
preg_match_all($pattern, $one, $matches);
print_r($matches);
EDIT: Simplified version without the match tags:
$pattern = '/(<p class="test">)(.*)(<\/p>)/i';
preg_match_all($pattern, $one, $matches);
print_r($matches);

Regexp in php: how do I filter dynamic strings like abc/123/...?

I am trying to filter out all characters before the first / sign. I have strings like
ABC/123/...
and I am trying to filter out ABC, 123 and ... into separate strings. I have alsmost succeeded with the parsing of the first letters before the / sign except that the / sign is part of the match, which I donĀ“t want to.
<?php
$string = "ABC/123/...";
$pattern = '/.*?\//';
preg_match($pattern, $string, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
The letters before the first/ can differ both in length and characters, so a string could also look like EEEE/1111/aaaa.
If you are trying to split the string using / as the delimiter, you can use explode.
$array = explode("/", $string);
And if you are looking only for the first element, you can use array_shift.
$array = array_shift(explode("/", $string));

preg matching and replacing elements

Hi how do I do a preg match on
$string1 = "[%refund%]processed_by"
$string2 = "[%refund%]date_sent"
I want to grab the bits inside %% and then remove the [%item%] altogether. leaving just the "proccessed_by" or "date_sent" I have had a go below but come a bit stuck.
$unprocessedString = "[%refund%]date_sent"
$match = preg_match('/^\[.+\]/', $unprocessedString);
$string = preg_replace('/^\[.+\]/', $unprocessedString);
echo $match; // this should output refund
echo $string; // this should output date_sent
Your problem is with your use of the preg_match function. It returns the number of matches found. But if you pass it a variable as a third parameter, it stores the matches for the entire pattern and its subpatterns in an array.
So you can capture both of the parts you want in subpatterns with preg_match, which means you don't need preg_replace:
$unprocessedString = "[%refund%]date_sent"
preg_match('/^\[%(.+)%\](.+)/', $unprocessedString, $matches);
echo $matches[1]; // outputs 'refund'
echo $matches[2]; // outputs 'date_sent'

Categories