What is the regex pattern to extract this URL? - php

I have a string:
href="javascript:KUBalloonOpen('0','http://img13.shop-pro.jp/PA01069/800/product/93163228.jpg',
I want to extract the URL from the above. I tried using this expression:
$regex = "KUBalloonOpen('0','(.*)',";
But it does not work. What is the correct regexp?

You need to escape single first ( your regex:
$str ="href=\"javascript:KUBalloonOpen('0','http://img13.shop-pro.jp/PA01069/800/product/93163228.jpg',";
$regex = "/KUBalloonOpen\('0','(.*)\',/";
preg_match($regex, $str, $match);
$match returns:
Array (
[0] => KUBalloonOpen('0','http://img13.shop-pro.jp/PA01069/800/product/93163228.jpg',
[1] => http://img13.shop-pro.jp/PA01069/800/product/93163228.jpg
)

Related

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

Get array of usernames from #Twitter like string

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

How to split a string into an array using a given regex expression

I am trying to explode / preg_split a string so that I get an array of all the values that are enclosed in ( ). I've tried the following code but I always get an empty array, I have tried many things but I cant seem to do it right
Could anyone spot what am I missing to get my desired output?
$pattern = "/^\(.*\)$/";
$string = "(y3,x3),(r4,t4)";
$output = preg_split($pattern, $string);
print_r($output);
Current output Array ( [0] => [1] => )
Desired output Array ( [0] => "(y3,x3)," [1] => "(r4,t4)" )
With preg_split() your regex should be matching the delimiters within the string to split the string into an array. Your regex is currently matching the values, and for that, you can use preg_match_all(), like so:
$pattern = "/\(.*?\)/";
$string = "(y3,x3),(r4,t4)";
preg_match_all($pattern, $string, $output);
print_r($output[0]);
This outputs:
Array
(
[0] => (y3,x3)
[1] => (r4,t4)
)
If you want to use preg_split(), you would want to match the , between ),(, but without consuming the parenthesis, like so:
$pattern = "/(?<=\)),(?=\()/";
$string = "(y3,x3),(r4,t4)";
$output = preg_split($pattern, $string);
print_r($output);
This uses a positive lookbehind and positive lookahead to find the , between the two parenthesis groups, and split on them. It also output the same as the above.
You can use a simple regex like \B,\B to split the string and improve the performance by avoiding lookahead or lookbehind regex.
\B is a non-word boundary so it will match only the , between ) and (
Here is a working example:
http://regex101.com/r/cV7bO7/1
$pattern = "/\B,\B/";
$string = "(y3,x3),(r4,t4),(r5,t5)";
$result = preg_split($pattern, $string);
$result will contain:
Array
(
[0] => (y3,x3)
[1] => (r4,t4)
[2] => (r5,t5)
)

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)

Categories