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
)
Related
I want to regex thread_id in this html code by using php
<a id="change_view" class="f_right" href="pm.php?view=chat&thread_id=462075438105382912">
I wrote this code however it return empty array to me
$success = preg_match_all('/pm\.php\?view=chat&thread_id=([^"]+)/', $con, $match2);
is there any problem in my php code ?
Well, you said it is giving you an empty array. But it is not. Here is the value returned by print_r()
Array
(
[0] => Array
(
[0] => pm.php?view=chat&thread_id=462075438105382912
)
[1] => Array
(
[0] => 462075438105382912
)
)
But It is not returning what you want it to. The regular expression to get string that comes after thread_id= and before & or " is :
/(?<=thread_id=).*(?=\"|&)/
Working example :
<?php
$con = '<a id="change_view" class="f_right" href="pm.php?view=chat&thread_id=462075438105382912">link</a>';
$match2 = Array();
preg_match_all('/(?<=thread_id=).*(?=\"|&)/', $val, $arr);
echo "<pre>";
print_r($arr);
echo "</pre>";
?>
Output :
Array
(
[0] => Array
(
[0] => 462075438105382912
)
)
If you're only looking for the thread_id, this should do it.
$success = preg_match_all('/(thread_id=)([\d]+)/', $con, $match2);
if (preg_match('/thread_id=[0-9]*/', $line, $matches))
$thread_id = $matches[0];
I have array with some strings and i want to use RegexIterator to replace some stuff on matched string but to also leave un-matched strings in array.
Here is my code:
$a = new ArrayIterator(array('LeaveThisInArray','value1', 'value2'));
$i = new RegexIterator($a, '/^(value)(\d+)/', RegexIterator::REPLACE);
$i->replacement = '$2:$1';
print_r(iterator_to_array($i));
And i get this as output:
Array
(
[0] => 1:value
[1] => 2:value
)
But what i wanted is this:
Array
(
[0] => LeaveThisInArray
[1] => 1:value
[2] => 2:value
)
Is there any flag i can set or something, because i cant find much in the spl documentation.
You can try with preg_replace
sample code:
$re = "/^(value)(\\d+)/m";
$str = "LeaveThisInArray\nvalue1\nvalue2";
$subst = '$2:$1';
$result = preg_replace($re, $subst, $str);
Here is online demo
Try with ^(value)(\d*) in your existing code.
The closest I can think about for this right now is like this:
$a = new ArrayIterator(array('LeaveThisInArray','value1', 'value2'));
$i = new RegexIterator($a, '/^(?:(value)(\d+))?/', RegexIterator::REPLACE);
$i->replacement = '$2$1';
print_r(iterator_to_array($i));
I have a string
$str = "recordsArray[]=3&recordsArray[]=1";
Is there any simple way to make this to an array other than exploding
this string at &? Im expecting a result like this print_r($str);
//expecting output
array('0' => '3','1' => '1');
Yes there is: parse_strDocs; Example (Demo):
parse_str("recordsArray[]=3&recordsArray[]=1", $test);
print_r($test);
Use preg_match_all like this:
$str = "recordsArray[]=3&recordsArray[]=1";
if ( preg_match_all('~\[\]=(\d+)~i', $str, $m) )
print_r ( $m[1] );
OUTPUT:
Array
(
[0] => 3
[1] => 1
)
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*(?=(?:\((.*)\))?)/'
I have the following string
{item1}home::::Home{/item1}{item2}contact_us::::Contact Us{/item2}{item3}.....
and so it goes on.
I need to split the string the following way
1=>{item1}home::::Home{/item1}
2=>{item2}contact_us::::Contact Us{/item2}
Is there a way?
$input = '{item1}home::::Home{/item1}{item2}contact_us::::Contact Us{/item2}{item3}.....';
$regex = '/{(\w+)}.*{\/\1}/';
preg_match_all($regex, $input, $matches);
print_r($matches[0]);
You could do it like this:
$text = "{item1}home::::Home{/item1}{item2}contact_us::::Contact Us{/item2}{item3}.....){/item3}";
preg_match_all('/{item\d}.+?{\/item\d}/', $text, $results);
var_dump($results) would produce:
Array
(
[0] => Array
(
[0] => {item1}home::::Home{/item1}
[1] => {item2}contact_us::::Contact Us{/item2}
[2] => {item3}.....){/item3}
)
)
Use preg_split() with the regex pattern /{.*?}.*?{\/.*?}/