if I have a string like 'foo(bar)', with the following code i can almost parse it the way i want:
$results = array();
preg_match( "/\w*(?=(\(.*\))?)/", 'foo(bar)', &$results );
print_r($results);
/*
Array
(
[0] => foo
[1] => (bar)
)
*/
How can I modify the regex to have bar instead of (bar)? Thanks
'/\w*(?=(?:\((.*)\))?)/'
Related
I'm basically looking for the opposite of http_build_query().
I have the following as a string:
foo=bar&bar[var]=foo
And I want the following (to pass into http_build_query):
array(
'foo' => 'bar',
'bar' => array(
'var' => 'foo',
)
)
You want parse_str(). Pass it an array as the 2nd parameter and it will extract variables from the query string you give it into the array:
<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str, $output);
print_r($output);
/*
Array
(
[first] => value
[arr] => Array
(
[0] => foo bar
[1] => baz
)
)
*/
Notice this is the very first related function listed on the http_build_query page.
Maybe it will work . parse_str 2nd parameter mandatory as of PHP 7.2+
parse_str(urldecode($builded_content), $output)
print_r($output)
I am working on a project and I can't figure out how to go about doing it. I'm trying to do with Regex but fairly new to it.
I have a string such as WHERE MyColumn='1's'' AND MyColumn='Test's''"
I have the following regex in PHP
$found = array();
preg_match("/\s=\s'.*'|\s.*='.*'\s/", $whereQuery, $found);
In my array I have the following
Array
(
[0] => MyColumn='1's''
)
So it's almost there, except I am expecting the following:
Array
(
[0] => MyColumn='1's''
[1] => MyColumn='Test's'
)
You should use preg_match_all with this simplified regex:
preg_match_all('/\w+\h*=\h*\S+/', $whereQuery, $found);
RegEx Demo
Hi look to print out:
test.net
tralala.net
from the following PHP code :
$query = "test.net dssdsd.com tralala.net";
$a = preg_grep('/(.*?)\.net$/', $query);
print_r($a);
But it gives me blank
Any idea ?
Use preg_match_all(), like this:
$query = "test.net dssdsd.com tralala.net";
preg_match_all('/\S+\.net\b/', $query, $matches);
print_r($matches[0]);
This outputs:
Array
(
[0] => test.net
[1] => tralala.net
)
I have a URL cleaned up to look like this:
/foo/bar
I tried this:
preg_match('#([a-zA-z0-9]+)#', $path, $matches);
But $matches is output as:
Array
(
[0] => /foo
[1] => foo
)
What regex can be used (i.e. with preg_match()) to get foo and bar?
You can best use explode for this:
print_r(explode('/',ltrim('/foo/bar','/')));
But if you want to do it with regex (for this very bad) just use preg_match_all():
preg_match_all('/([a-zA-Z0-9]+)/','/foo/bar', $matches);
echo '<pre>';
print_r($matches[0]);
For more information look at: http://nl3.php.net/manual/en/function.preg-match-all.php
Both result in:
Array
(
[0] => foo
[1] => bar
)
Use PHP explode():
$arr = explode('/', substr($str,1) );
returns
Array(
[0] = 'foo';
[1] = 'bar'
)
I'm basically looking for the opposite of http_build_query().
I have the following as a string:
foo=bar&bar[var]=foo
And I want the following (to pass into http_build_query):
array(
'foo' => 'bar',
'bar' => array(
'var' => 'foo',
)
)
You want parse_str(). Pass it an array as the 2nd parameter and it will extract variables from the query string you give it into the array:
<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str, $output);
print_r($output);
/*
Array
(
[first] => value
[arr] => Array
(
[0] => foo bar
[1] => baz
)
)
*/
Notice this is the very first related function listed on the http_build_query page.
Maybe it will work . parse_str 2nd parameter mandatory as of PHP 7.2+
parse_str(urldecode($builded_content), $output)
print_r($output)