Get array of usernames from #Twitter like string - php

How do I get an array of usernames from a string tagged like in Twitter with the '#' prefix using regex or similar?
For example:
Input:
hello #person my name is #joebloggs
Output (array):
['person', 'joebloggs']

Another solution
#[^\s]+
Usage:
$string = 'hello #person my name is #joebloggs';
$pattern = '/#[^\s]+/';
preg_match_all($pattern, $string, $matches);
print_r($matches[0]);
Output:
Array
(
[0] => #person
[1] => #joebloggs
)

Do this:
$regex = '~#\K\S+~';
preg_match_all($regex, $yourstring, $matches);
print_r($matches[0]);
See the matches in the Regex Demo.
Explanation
# matches the AT (but it will not be returned)
The \K tells the engine to drop what was matched so far from the final match it returns
\S+ matches any non-space characters

use this :
<?php
$re = "/(?<=#)[^\s]+/";
$str = "asdasd asda 232 #asdasd sd232 soi #other asdnasda asjdajh #asdasd";
preg_match_all($re, $str, $matches);
print_r($matches);
demo here : https://eval.in/173103
output:
Array
(
[0] => Array
(
[0] => asdasd
[1] => other
[2] => asdasd
)
)

Related

PHP preg_match_all grab info between matches

I can't figure this out. How do I grab the information between the regex matches?
My issue seems to be that there are newlines in the string. If I compress it to one line per "Title", some of my attempts work.
I want an output that looks like this:
Array
(
[0] => Array
(
[0] => Title1#
[1] => - contenta
- contentb
)
[1] => Array
(
[0] => Sometitle2#
[1] => - contenta
- contentb
)
[2] => Array
(
[0] => ABC3#
[1] => - asdfasdfasdf
- random stuff
more
something
)
)
Here are some of my attempts so far (I even tried some preg_split here), with example the string.
<?php
$str = 'Title1#
-contenta
-contentb
Sometitle2#
-contenta
-contentb
ABC3#
- asdfasdfasdf
- random stuff
more
something';
$re = '/[A-Za-z]{1,10}[0-9]?#\s?(.*\s)/m';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
print_r($matches);
$re = '/([A-Za-z]{1,10}[0-9]?#\s?)/m';
$keywords = preg_split($re, $str,null,PREG_SPLIT_DELIM_CAPTURE);
print_r($keywords);
$parts = preg_split('/([A-Za-z]{1,10}[0-9]?#\s?)/m', $str,null,PREG_SPLIT_DELIM_CAPTURE);
print_r($parts);
?>
Thanks!
You may use this regex in preg_match_all:
$re = '~(?ms)^([^#\n]+#)\s+(.*?(?=\n+[^#\n]*#\s|\z))~';
RegEx Demo
RegEx Details:
(?ms): Enable MULTILINE and DOTALL modes
^; Line start
([^#\n]+#)\s+: First capture group. Match a line that ends with #
(.*?(?=\n+[^#\n]*#\s|\z)): Second capture group. Match 0 or more characters that either have line with # ahead or \z.
Cude:
$re = '/(?ms)^([^#\n]+#)\s+(.*?(?=\n+[^#\n]*#\s|\z))/';
$str = 'Title1#
-contenta
-contentb
Sometitle2#
-contenta
-contentb
ABC3#
- asdfasdfasdf
- random stuff
more
something';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);

How to get Variable from Regex in PHP?

How can I get only the Name/Variable which is "regexed"? Like in this case the $1 or $0 in the anchor's href?
When I try to echo the $1 or $0 I get a Syntax Error because it's a Number.
At the Moment the $str is a whole Text.
function convertHashtags($str){
$regex = "/#+([a-zA-Z0-9_]+)/";
$str = preg_replace($regex, '$0', $str);
return($str);
}
Simple use preg_match before preg_replace, eg
preg_match($regex, $str, $matches);
Assuming the pattern actually matched, you should have the results in $matches[0] and $matches[1] which are the equivalent of $0 and $1 in the replace string.
FYI, the $n tokens in the replacement string are not variables though I can see how that can be confusing. They are simply references to matched groups (or the entire match in the case of $0) in the regex.
See http://php.net/manual/function.preg-replace.php#refsect1-function.preg-replace-parameters
To find multiple matches in $str, use preg_match_all(). It's almost the same only it populates $matches with a collection of matches. Use the PREG_SET_ORDER flag as the 4th argument to make the array workable. For example...
$str = ' xD #lol and #testing';
$regex = '/#(\w+)/';
preg_match_all($regex, $str, $allMatches, PREG_SET_ORDER);
print_r($allMatches);
produces...
Array
(
[0] => Array
(
[0] => #lol
[1] => lol
)
[1] => Array
(
[0] => #testing
[1] => testing
)
)

preg_match unexpected behaviour

This is my code:
preg_match('/(?<=groups\\/)\\d+/im', $_SERVER['REQUEST_URI'], $matches)
When $_SERVER['REQUEST_URI'] = "/groups/S14022/", $matches is empty.
When $_SERVER['REQUEST_URI'] = "/groups/S14022", $matches[0] gets me "S14022".
What do I have to change on the regex to match both cases?
Try this way,
$re = "/(?<=\\/groups\\/)S\\d+\\/?/m";
$str = "/groups/S14022/\n/groups/S14022";
preg_match_all($re, $str, $matches);
See Demo https://regex101.com/r/qP2rO5/1
I think all you need is this regex (as in your regex, you are missing S):
(?<=groups\/)S\d+
PHP code:
$re = "/(?<=groups\\/)S\\d+/im";
$str = "/groups/S14022/\n/groups/S14022";
preg_match_all($re, $str, $matches);
print_r($matches);
Output of the sample program:
Array
(
[0] => Array
(
[0] => S14022
[1] => S14022
)
)

Custom regular expression pattern

What's the right pattern to obtain something like that using preg_split.
Input:
Src.[VALUE1] + abs(Src.[VALUE2])
Output:
Array (
[0] => Src.[VALUE1]
[1] => Src.[VALUE2]
)
Instead of using preg_split, using preg_match_all makes more sense in this case:
preg_match_all('/\w+\.\[\w+\]/', $str, $matches);
$matches = $matches[0];
Result of $matches:
Array
(
[0] => Src.[VALUE1]
[1] => Src.[VALUE2]
)
This regex should be fine
Src\.\[[^\]]+\]
But instead of preg_split I'd suggest using preg_match_all
$string = 'Src.[VALUE1] + abs(Src.[VALUE2])';
$matches = array();
preg_match_all('/Src\.\[[^\]]+\]/', $string, $matches);
All matches you're looking for will be bound to $matches[0] array.
I guess preg_match_all is what you want. This works -
$string = "Src.[VALUE1] + abs(Src.[VALUE2])";
$regex = "/Src\.\[.*?\]/";
preg_match_all($regex, $string, $matches);
var_dump($matches[0]);
/*
OUTPUT
*/
array
0 => string 'Src.[VALUE1]' (length=12)
1 => string 'Src.[VALUE2]' (length=12)

PHP: preg_match `eats` my string

Parsing code
$str = 'My name is Michael. I am a sportsman!';
preg_match('|My name is (.*?)\. I am a (.*?)|', $str, $m);
print_r($m);
returns me string:
Array ( [0] => My name is Michael. I am a [1] => Michael [2] => )
Where is sportsman?
That's because the expression is not anchored, or rather, the second (.*?) doesn't have a look-ahead set and therefore matches nothing); you should add the end-of-string anchor like this:
preg_match('|My name is (.*?)\. I am a (.*?)$|', $str, $m);
^
You could also make the second expression greedy:
preg_match('|My name is (.*?)\. I am a (.*)|', $str, $m);
^

Categories