php Regular expressions basic - php

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

Related

Get portion of PHP variable

I need to get only a part of a PHP variable.
$somevar = "sd300.somedata.cd.vm.someotherdata.cd.vm";
Now, I cannot figure out, but I only need this first part and do away with the last part, then save the variable again as $somevar
I only need to capture sd300.somdata.cd.vm
I've tried preg_replace but just cannot figure it out.
Does anyone have an idea?
If you still wanted to use preg_replace:
$somevar = "sd300.somedata.cd.vm.someotherdata.cd.vm";
$somevar = preg_replace("/(.*\..*\..*\..*)\..*\..*\..*/", '$1', $somevar);
var_dump($somevar);
Using preg_match_all() you can do this:
$somevar = "sd300.somedata.cd.vm.someotherdata.cd.vm";
$pattern = '/((.*?\.)(cd\.vm))/';
preg_match_all($pattern, $somevar, $matches);
print_r($matches);
Returns
Array
(
[0] => Array
(
[0] => sd300.somedata.cd.vm
[1] => .someotherdata.cd.vm
)
[1] => Array
(
[0] => sd300.somedata.cd.vm
[1] => .someotherdata.cd.vm
)
[2] => Array
(
[0] => sd300.somedata.
[1] => .someotherdata.
)
[3] => Array
(
[0] => cd.vm
[1] => cd.vm
)
)
$matches[0][0] contains the output you requested in your question.
The below logic would be helpful
$somevar = "sd300.somedata.cd.vm.someotherdata.cd.vm";
echo (implode('.',current(array_chunk(explode('.',$somevar),4))));
You can try to do this:
$somevar = "sd300.somedata.cd.vm.someotherdata.cd.vm";
$delimiter = '.cd.vm';
$temp = explode($delimiter, $somevar);
Your searched value:
$value = $temp[0] . $delimiter;
You didn't give a lot of information that is why I am guessing.

how to put the fetched values in to variable with comma separation?

while working on tags process i got one problem . here i am using preg_match_all method for selecting particular tag.
after using that i am getting data like that . here posting the php code and output please check.
<?php
$comment = $_POST['comment'];
preg_match_all("/(#\w+)/", $comment, $matches);
echo "<pre>"; print_r($matches);
?>
output:
<pre>Array
(
[0] => Array
(
[0] => #name
[1] => #variables
)
[1] => Array
(
[0] => #name
[1] => #variables
)
)
here i am getting doubt how to convert this array with comma separation.
expecting:
$tagging = (#name, #variables);
any one knows please help me how to solve like that.
$tagging = implode (",", $matches[1]); // '#name,#variables'
Try this:
<?php
$comment = $_POST['comment'];
preg_match_all("/(#\w+)/", $comment, $matches);
foreach($matches as $val){
echo implode (",", $val);
//echo '(' . implode (",", $val) . ')';
}
?>
Un-comment second line if you want paranthesis as well

php array_filter send empty value

I have this array:
Array (
[0] => http://localhost/cms/uploads/files/1/images/hd-allpaper-40.jpg
[1] => http://localhost/cms/uploads/files/1/images/IMG_6217.JPG
[2] =>
)
Now with this code, serialize data and insert to my database :
foreach(array_filter($_POST['image_url']) as $image_url) {
if(!empty($image_url)) {
$url = parse_url(array_shift($_POST['image_url']));
preg_match('/[0-99999]\/.*/', $url['path'], $matches);
$value['gallery_data'] = serialize((array(
array_filter($matches[0]),
array_values($_POST['image_title']),
array_values($_POST['image_alt'])
)));
print_r $value['gallery_data'];
}
}
But when i print_r $value['gallery_data']; matches[0] send empty value.
EDIT : when replace $matches[0] with $_POST['image_url'] my code worked true.
var_dump(matches[0]):string(28) "1/images/hd-wallpaper-40.jpg" string(21) "1/images/IMG_6217.JPG"
how do fix this problem ?!

Find occurences of domain extension in a string PHP

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
)

PHP: How to implode these array values?

I have these code:
$arr = array($check);
When I try to
echo "<pre>";
die(print_r($arr));
it gives me result in the Firebug
<pre>Array
(
[0] => Array
(
[0] => 2
[1] => 1
)
)
Now, I want an output that will give me '2,1'.
How could I do that in PHP? Do I need to use implode?
Yes you need to implode:
echo implode(',', $check);
You don't need to put it inside an array:
$arr = array($check); // no need

Categories