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

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]

Related

Perform word match in a String until another word is found in the String

I'm using preg_match_all to perform all match in a String
$match_this = '/cola/';
$sentence = 'cola this cola is a nice cola';
if(preg_match_all($match_this, $sentence, $matches)){
echo 'match found'.'<br>';
print_r($matches[0]);
}
But I want this match performing operation to stop when I encounter the word nice and $matches array shouldn't store any more matched word after that.
How the code can be modified for this ?
There can be multiple times 'cola' comes before 'nice'. This is just
an example sentence. Again 'cola' and 'nice' are just example words.
The words to match and where to stop are randomly picked from
database. This code is for a word game.
You could use positive lookahead:
$match_this = '\bcola\d\b';
$until = '\bnice\b';
$sentence = 'cola1 this cola2 is a nice cola3';
if(preg_match_all("/$match_this(?=.*$until)/", $sentence, $matches)){
print_r($matches[0]);
}
Output:
Array
(
[0] => cola1
[1] => cola2
)
I've added a number at the end of each cola to be sure it matches only the ones that are before the word nice.
I've also added word boudaries arround the words to match.
Finally the code is:
$match_this = '\bcola\b';
$until = '\bnice\b';
$sentence = 'cola this cola is a nice cola';
if(preg_match_all("/$match_this(?=.*$until)/", $sentence, $matches)){
print_r($matches[0]);
}
First get the offset of nice, then run the preg match on the substring before it.
$sentence = 'cola this cola is a nice cola';
$match_this = '/nice/';
if(preg_match($match_this, $sentence, $matches, PREG_OFFSET_CAPTURE)){
$niceOffset = $matches[0][1];
$match_this = '/cola/';
if(preg_match_all($match_this, substr($sentence, 0, $niceOffset), $matches2, PREG_OFFSET_CAPTURE)){
var_dump($matches2);
}
}

Get numbers after string php regex

I need a regex that can actually get any number that is inserted after "ab" and "cr". For example, I have a string like this:
rw200-208-ab66
fg200-cr30-201
I need to print ab66 and cr30.
I have tried using strpos:
if (strpos($part,'ab') !== false) {
$a = explode("ab", $part);
echo 'ab'.$a[1];
}
That does not work for the second item.
You could use \K to discard the previously matched chars from printing at the final. The below regex would give you the number which exists next to ab
or cr.
(?:ab|cr)\K\d+
To get the number with alphabets also, use
preg_match_all('~(?:ab|cr)\d+~', $str, $match);
Use this regex:
(?>ab|cr)\d+
See IDEONE demo:
$re = "#(?>ab|cr)\d+#";
$str = "rw200-208-ab66\nfg200-cr30-201";
preg_match_all($re, $str, $matches);
print_r($matches[0]);
Output:
Array
(
[0] => ab66
[1] => cr30
)

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

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

Categories