Extract part of a string before and after a special character - php

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

Related

PHP substr - extract numbers from string

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

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

regex find usernames starting with # php

so i want to find usernames in a string and put them in an array, i've made the regex and it returns the match, but when there are 2 matches it only puts the first one in the array. Can anyone see what is wrong with my regex?
$reactie = 'hey #sjerd and #jeska';
$pattern = '/#\w*/';
preg_match($pattern, $reactie, $matches);
print_r($matches);
You need to use preg_match_all with correct regex with word boundary:
$reactie = 'hey #sjerd and #jeska';
$pattern = '/#\w+\b/';
preg_match_all($pattern, $reactie, $matches);
print_r($matches[0]);

How to get word in php which contains colon(:)?

I have a word Hello How are you :chinu i am :good i want to get the word which contains : like :chinu and :good
My code:
<?php
//$string='Hello How are you :chinu i am :good';
//echo strtok($string, ':');
$string='Hello How are you :chinu i am :good';
preg_match('/:([:^]*)/', $string, $matches);
print_r($matches);
?>
Above code i am getting Array ( [0] => : [1] => ) But not getting the exact text. Please help me.
Thanks
Chinu
To get all matches you need to use preg_match_all(). As far as your regular expression goes your negated class is backwards; matching any character of: :, ^ "zero or more" times and will not match what you expect.
You stated in the comments about the "records" being printed twice, this is because you print the $matches array itself instead of printing the group index which only displays the match results.
preg_match_all('/:\S+/', $string, $matches);
print_r($matches[0]);
:\S+
Try this.See demo.
http://regex101.com/r/tF5fT5/43
$re = "/:\\S+/im";
$str = "Hello How are you :chinu i am :good";
preg_match_all($re, $str, $matches);
So you would need to do something like the following to match the characters up to the space:
preg_match_all('/:[^ ]+/', $string, $matches);
or if you're looking for alpha-only characters you might use the following:
preg_match_all('/:[A-Za-z]+/', $string, $matches);
The array you would look for would be $matches[0] at this point.
print_r($matches)
print_r($matches[0])
You can always reassign the matches sub array with something like this:
$matchesArray = $matches[0]

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.

Categories