Find all words/string in an expression - php

Lets say I have expression like
$string = ( score + total-score - total-min_score) / papoy
I want to able to extract those ALL words/term into an array ( words with/without dash and underscore )
I tried like (I'm not so good with regex)
preg_match("(\w+-_)",$string,$matches);
But it only return me the first match. How could I get all matches?

You need to use preg_match_all function.
preg_match_all('~[\w-]+~',$string,$matches);
OR
preg_match_all('~\w+(?:-\w+)*~', $string, $matches);
DEMO

Related

Php preg_match multiple occurrences, return unique array

I want to be able to extract certain parts of the string and return unique array. Here is my string:
$string = "
<div> some text goes here... **css/method|1|2**</div>
<div>**php/method|3|4**</div>
<div>**html|method|6|9** and more text here</div>
<div>**html/method|2|5**</div>
";
using preg_match_all()
$pattern = "/**(.*?)**/";
preg_match_all($pattern, $string, $matches);
I can extract all the parts from the string, but I need to go step further, and only return the following:
css, php and html.
the final array should look like this:
$result = array("css", "php", "html");
So basically, I need to eliminate duplicate values in this case "html", as well as extract each value before backslash or pipe. I don't care about method parts as well as what goes after.
The solution using preg_match_all and array_unique functions:
preg_match_all("~\*\*([^/|*]+)(?=[/|])~", $string, $matches);
$result = array_unique($matches[1]);
print_r($result);
The output:
Array
(
[0] => css
[1] => php
[2] => html
)
(?=[/|]) - positive lookahead assertion which matches word that is followed by one of the characters /|
Update: to ignore tags from match update regex pattern with the following ~\*\*([^/|*<>]+)(?=[/|])~

Match a string using some regular expression

I've a string that looks like this:
089 / 23249841noch not deposited
and I want to extract the following portion from the string:
089 / 23249841
How can I do this using PHP and regular expressions?
Assuming you want to match everything before the first letter,
preg_match("/(^[^a-z]+)/i", "089 / 23249841noch not deposited", $match)
$match would then contain,
Array
(
[0] => 089 / 23249841
[1] => 089 / 23249841
)
With only one example, it's a little tricky to write a proper regex for that. However, this one should work:
[0-9 /]+
Or, in full PHP code:
$str = '089 / 23249841noch not deposited';
$matches = array();
if (preg_match('[0-9 /]+', $str, $matches)) {
var_dump($matches);
}

Get next word after preg match with PHP

How can I get the next word after pregmatch with PHP.
For example, If I have a string like this:
"This is a string, keyword next, some more text. keyword next-word."
I want to use a preg_match to get the next word after “keyword”, including if the word is hyphenated.
So in the case above, I want to return “next” and ”next-word”
I’ve tried :
$string = "This is a string, keyword next, some more text. keyword next-word.";
$keywords = preg_split("/(?<=\keyword\s)(\w+)/", $string);
print_r($keywords);
Which just returns everything and doesn’t seem to work at all.
Any help is much appreciated.
Using your example this should work using preg_match_all:
// Set the test string.
$string = "This is a string, keyword next, some more text. keyword next-word. keyword another_word. Okay, keyword do-rae-mi-fa_so_la.";
// Set the regex.
$regex = '/(?<=\bkeyword\s)(?:[\w-]+)/is';
// Run the regex with preg_match_all.
preg_match_all($regex, $string, $matches);
// Dump the resulst for testing.
echo '<pre>';
print_r($matches);
echo '</pre>';
And the results I get are:
Array
(
[0] => Array
(
[0] => next
[1] => next-word
[2] => another_word
[3] => do-rae-mi-fa_so_la
)
)
Positive look behind is what you are looking for:
(?<=\bkeyword\s)([a-zA-Z-]+)
Should work perfect with preg_match. Use g modifier to catch all matches.
Demo
Reference Question: How to match the first word after an expression with regex?
While regex is powerful, it's also for most of us hard to debug and memorize.
On this particular case, Get next word after ... match with PHP, which is a very common string operation.
Simply, by exploding the string in an array, and searching the index. This is useful because we can specify how much words forward or backward.
This match the first occurrence + 1 word:
<?php
$string = explode(" ","This is a string, keyword next, some more text. keyword next-word.");
echo $string[array_search("keyword",$string) + 1];
/* OUTPUT next, *
Run it online
By reversing the array, we can catch the last occurrence - 1 word:
<?php
$string = array_reverse(explode(" ","This is a string, keyword next, some more text. keyword next-word."));
echo $string[array_search("keyword",$string) - 1];
/* OUTPUT next-word. */
Run it online
This is good for performances if we are making multiples searches, but of course the length of the string must be kept short (whole string in memory).

Manipulating string using PHP

In PHP, I have strings like so:
$string = "This is a 123 test (your) string (for example 1234) to test.";
From that string, I'd like to get the words inside the () with the numbers. I've tried using explode but since I have 2 word/group of strings enclosed in the parentheses, I end up getting (your) instead of (for example 1234). I've also used substr like so:
substr($string, -20)
This works most of the time but the problem with this is, there are instances that the string is shorter so it ends up getting even the unwanted string. I've also tried using regular expression in which I set something like so:
/[^for]/
but that did not work either. The string I want to get always starts with "for" but the length varies. How do I manipulate php so that I can get only the string enclosed inside the parentheses that starts with the word for?
I might use preg_match() in this case.
preg_match("#\((for.*?)\)#",$string,$matches);
Any matches found would be stored in $matches.
Use the following regular expression:
(\(for.*?\))
It will capture patterns like:
(for)
(foremost)
(for example)
(for 1)
(for: 1000)
A sample PHP code:
$pattern = '/(\(for.*?\))/';
$result = preg_match_all(
$pattern,
" text (for example 1000) words (for: 20) other words",
$matches
);
if ( $result > 0 ) {
print_r( $matches );
}
Above print_r( $matches ) result:
Array
(
[0] => Array
(
[0] => (for example 1000)
[1] => (for: 20)
)
[1] => Array
(
[0] => (for example 1000)
[1] => (for: 20)
)
)
Use preg_match for regular expression
$matches = array();
$pattern = '/^for/i';
preg_match($pattern,$string,$matches);
pirnt_r($matches);
If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.
$matches = array();
preg_match("/(\(for[\w\d\s]+\))/i",$string,$matches);
var_dump($matches);

PHP preg_match matches

I'm trying to get all the matches from string:
$string = '[RAND_15]d4trg[RAND_23]';
with preg_match like this:
$match = array();
preg_match('#\[RAND_.*]#', $string, $match);
but after that $match array looks like this:
Array ( [0] => [RAND_15]d4trg[RAND_23] )
What should I do to get both occurrences as 2 separate elements in $match array? I would like to get result like this:
$match[0] = [RAND_15];
$match[1] = [RAND_23];
Use ...
$match = array();
preg_match_all('#\[RAND_.*?]#', $string, $match);
... instead. ? modifier will make the pattern become 'lazy', matching the shortest possible substring. Without it the pattern will try to cover the maximum distance possible, and technically, [RAND_15]d4trg[RAND_23] does match the pattern.
Another way is restricting the set of characters to match with negated character class:
$match = array();
preg_match_all('#\[RAND_[^]]*]#', $string, $match);
This way we won't have to turn the quantifier into a lazy one, as [^]] character class will stop matching at the first ] symbol.
Still, to catch all the matches you should use preg_match_all instead of preg_match. Here's the demo illustrating the difference.

Categories