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

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

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.

php Regular expressions basic

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

PHP ARRAY_PUSH strips html tag

I have a string which i am trying to split and then add a span tag on every 2 words.
When I split the string and try to use array_push to create a new array, my html tags disappears.
Here is my function:
public function splitString(){
$string = Sample sentence;
$newHeader = array();
$parts = preg_split('/\s+/', $string);
$num = 1;
foreach($parts as $str){
if($num % 2 == 0){
array_push($newHeader, "<span>".$str."</span>");
}else{
array_push($newHeader, $str);
}
$num++;
}
return $newHeader;
}
When I call that function the result i get is
Array ( [0] => Sample [1] => sentence )
I am looking for:
Array ( [0] => Sample [1] => <span>sentence</span> )
What I am doing wrong? Please help
Thank you in advance
First, if you haven't corrected, just like #Fred said in the comments, you should quote your strings in that function:
$string = 'Sample sentence';
Second, it works. array_push() does not strip your tags. You are just presented with a print_r() on the browser but its there along with the word.
Array ( [0] => Sample [1] => sentence )
If look at it in the view source. This is what it looks like:
print_r(splitString());
Array
(
[0] => Sample
[1] => <span>sentence</span>
)
You just don't see it visually on the browser but the tags are there.
If you try to add this up:
array_push($newHeader, "<span style='color: red;'>".$str."</span>");
You'll see the style. Try it :)

Match String and Get Variable PHP

I would like to be able to use this string to pull off a certain piece of data from a database '{my-id-1}' so basically if this is found in the text '{my-id-*}' then get the id (eg. if {is-id-1} then ID is 1) and then I can run some code with that ID.
So I've got it so I can get the ID from the braces, but I'm not sure how to replace that within the text.
<?php
$text = "test 1 dfhjsdh sdjkfhksdhfkj skjh {is-id-1} sdfhskdfh sdfsdjfhksd fjksdfhksd {is-id-2}";
preg_match_all('/{is-id-+(.*?)}/',$text, $matches);
print_r ($matches);
$replacewiththis = "this has been replaced, it was id: " . $idhere;
$text = preg_replace('/{is-id-+(.*?)}/', $replacewiththis, $text);
echo $text;
?>
The Array for the matches outputs:
Array (
[0] => Array (
[0] => {is-id-1}
[1] => {is-id-2}
)
[1] => Array (
[0] => 1
[1] => 2
)
)
I'm stuck now and not sure how to can process each of the braces. Can anyone give me a hand?
Thanks.
I am not sure I understood well what you want, but I think this is it:
foreach($matches[1] as $match){
$replacewiththis = "this has been replaced, it was id: $match";
$text=str_replace('{is-id-'.$match.'}', $replacewiththis, $text);
}
echo $text;

getting null values while reading from csv file

I have a csv file which has a word for each row like this
word1
word2
word3
And have the following code to read the data:
$arr = array();
$fh = fopen('path/to/file', 'r');
while (($rows = fgetcsv($fh)) !== false) {
print_r($rows);
if (!in_array($rows[0], $arr))
$arr[] = "'".trim($rows[0])."'";
}
fclose($fh);
print_r($arr);
The problem is im getting empty strings/null for $rows[0] and im quite sure the data is there.
Array ( [0] => )
Array ( [0] => )
Array ( [0] => )
Array ( [0] => '' [1] => '' [2] => '' [3] => '' )
Any help would be greatly appreciated.
You're trying to read the words into an array? Replace all that with just:
$arr = file('/path/to/file');
print_r($arr);
Cannot reproduce. I'm assuming there is something wrong with your file format. Most likely there is an empty line on top (which I'm not quite sure, but might fail the !== false test).
Better try:
$arr = array_map("str_getcsv", file("words.csv"));
print_r($arr);
And if you need to re-add the quotes, do a separate loop.
The file reading part has already been answered, but about your loop to add the quotes, you could use array_walk():
function add_quotes(&$item)
{
$item = "'" . $item . "'";
}
array_walk($arr, "add_quotes");

Categories