PHP substr - extract numbers from string - php

I need to extract the highlighted numbers from each line in a CSV file.
Currently I am looping though the lines & splitting the line on the / character as this only appears once in each row, but how do I remove everything around these numbers so I am left with:
9/10
10/11
11/12
...

If you want to only get the numbers, you could do a preg_match
$re = '/(\d+\/\d+)/s';
$str = 'dfsadsfadsfads~~9/10~~lfkjdskfds';
preg_match($re, $str, $matches);
but if you are able to get the entire doc as a single string, you could do a preg_match_all
$re = '/(\d+\/\d+)/s';
$str = 'dfsadsfadsfads~~9/10~~lfkjdskfds\ndfsadsfadsfads~~9/10~~lfkjdskfds\n';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
then loop on the $matches

Related

Extract part of a string before and after a special character

I need to extract and show some words before and after a special character.
For example, this text: Anywords1 Anywords2 – Anywords3 Anywords4 (39948)
I need to show (PHP):
Anywords1 Anywords2
Anywords3 Anywords4
Many Thanks!
Going by your example you could use:
$re = '/([a-zA-Z ])+/m';
$str = 'Anywords Anywords – Anywords Anywords (39948)';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);

Using regex in php to find string values between characters multiple times

Here I have a string, "Hello World! I am trying out regex in PHP!". What I want to do is retrieve string values between a set of characters. In this example, the characters are ** **
$str = "**Hello World!** I am trying out regex in PHP!";
preg_match('#\*\*(.*)\*\*#Us', $str, $match);
echo $match[1];
This will echo out "Hello World!", but I want to echo out several matches:
$str = "**Hello World!** I am trying out **regex in PHP!**";
How would I be able to do so? I tried using preg_match_all() but I don't think I was using it properly, or that it would work at all in this case.
You can use:
$str = "**Hello World!** I am trying out **regex in PHP!**";
preg_match_all('/\*{2}([^*]*)\*{2}/', $str, $m);
print_r($m[1]);
Array
(
[0] => Hello World!
[1] => regex in PHP!
)
Even your regex #\*\*(.*)\*\*#Us should work with this but my suggested regex is little more efficient due to negation based pattern [^*]*
You got 1 match owing to using preg_match.You should use preg_match_all Here is another pattern.It uses word non word match between the delimiters
<?php
$str = "**Hello World!** I am trying out **regex in PHP!**";
$regex='/\*\*([\w\W]*)\*\*/iU';
preg_match_all($regex, $str, $m);
print_r($m[1]);
I suggest you to use a non-greedy form of regex. Because i think you want to match also the contents (text inside **) where the single * resides.
$str = "**Hello World!** I am trying out **regex in PHP!**";
preg_match_all('~\*\*(.*?)\*\*~', $str, $matches);
print_r($matches[1]);
DEMO

PHP preg_match everything except commas?

$regex = "/(.+),(.+);/";
$input = "somestring, 234, sometring5";
preg_match_all($regex, $input, $matches, PREG_SET_ORDER);
I've tried to make it like this:
$regex = "/(.^,+),(.^,+);/";
$input = "somestring, 234, sometring5";
preg_match_all($regex, $input, $matches, PREG_SET_ORDER);
But it doesn't work, because I thought that ^, means except commas, but why it doesn't work?
Because I want to group them by commas, but the commas are symbols itself that the parser gets how to avoid this?
You could just split the string on , and trim the result:
$matches = array_map('trim', explode(',', $input));
Here's a modified version of your RegEx, with some explanations for each modifier.
And for those not willing to visit the link:
A RegEx to match all words in a sentence is /([a-zA-Z0-9]*)/g
[a-zA-Z0-9] means match all non symbol characters (a-z, A-Z and 0-9)
* means match it as many repeating times as possible
g modifier (the /g at the end) means match as many as possible inside the string; don't just stop at the first one.
$regex = "/([^,])/g";
$input = "somestring, 234, sometring5";
preg_match_all($regex, $input, $matches, PREG_SET_ORDER);
will get you everything except commas.

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

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

Categories