I'm planning to build web app and i heve question how to create mentions for name "#myname" like facebook or twitter
Find them in a string with this regex....
$str = 'Yo #bob, what\'s up? I have a new email, tom#bob.com, tell #john too, from #alex';
preg_match_all('/\s#(?P<mention>\w+?)\b/', $str, $mentions);
var_dump($mentions);
Output
array(3) {
[0]=>
array(3) {
[0]=>
string(5) " #bob"
[1]=>
string(6) " #john"
[2]=>
string(6) " #alex"
}
["mention"]=>
array(3) {
[0]=>
string(3) "bob"
[1]=>
string(4) "john"
[2]=>
string(4) "alex"
}
[1]=>
array(3) {
[0]=>
string(3) "bob"
[1]=>
string(4) "john"
[2]=>
string(4) "alex"
}
}
Of course, you could real time detect them in a string in JavaScript, just change that regex to a JavaScript one.
Then, you would look up your database based on the tagged name, and then do what you need to do!
You could cut down on requests by limiting your regex to what makes a valid username, e.g. /\w{6,}/.
function mention($txt)
{
$txt = ' '.$txt;
preg_match_all('/\s#(?P<mention>\w+?)\b/', $txt, $mentions);
echo "<pre>";
print_r($mentions);
if (isset($mentions[0])) {
foreach ($mentions[0] as $key => $value) {
$mention = strtolower(str_replace(array("#"," "), "", $value));
$txt = str_replace($value, ' '.$mention.'', $txt);
}
}
return trim($txt);
}
Related
I have the next array with data (which is dynamically generated).
Now I want to do some Magic and tweak the array.
array(1) {
["table"]=>
array(3) {
["header"]=>
array(4) {
[0]=>
array(1) {
["c"]=>
string(4) "Naam"
}
[1]=>
array(1) {
["c"]=>
string(7) "Functie"
}
[2]=>
array(1) {
["c"]=>
string(13) "Nevenfuncties"
}
[3]=>
array(1) {
["c"]=>
string(34) " commissies"
}
}
["caption"]=>
bool(false)
["body"]=>
array(3) {
[0]=>
array(4) {
[0]=>
array(1) {
["c"]=>
string(16) "*|class:orange|*"
}
[1]=>
array(1) {
["c"]=>
string(6) "dsasad"
}
[2]=>
array(1) {
["c"]=>
string(0) ""
}
[3]=>
array(1) {
["c"]=>
string(0) ""
}
}
[1]=>
array(4) {
[0]=>
array(1) {
["c"]=>
string(4) "brrr"
}
[1]=>
array(1) {
["c"]=>
string(6) "adsdsa"
}
[2]=>
array(1) {
["c"]=>
string(0) ""
}
[3]=>
array(1) {
["c"]=>
string(0) ""
}
}
[2]=>
array(4) {
[0]=>
array(1) {
["c"]=>
string(6) "dsasad"
}
[1]=>
array(1) {
["c"]=>
string(6) "dsadas"
}
[2]=>
array(1) {
["c"]=>
string(4) "dsad"
}
[3]=>
array(1) {
["c"]=>
string(0) ""
}
}
}
}
}
When we look at the ['header'] it contains ['c'] (the cell data). This can be text, but also a tag.
For example: *|class:orange|* here some text.
Now I want to split those up and overwrite the ['c'] if it contains '|class:orange|'.
So when you have this:
array(1) {
["c"]=>
string(7) "*|class:orange|* hello"
}
It would become this:
array(2) {
["c"]=>
string(7) "hello",
["class"]=>
string(7) "orange",
}
This way I could split the class and add it to the array. But I am stuck at the preg_match.
foreach ($table as &$row) {
foreach ($row['header'] as &$header) {
// $header['class'] = 123;
preg_match('/\*\|class:([^\|])\|\*/', $header['c'], $matches);
}
}
I need to do 2 things
Add an attribute to the array ($header['class']) with the class after class:example.
I need to replace the $header['c'] so it does not contain *|class:orange|* and only the rest of the text.
No need for regexp (yet). Tags can be found with delimiter positioning:
foreach ($row['header'] as &$header) {
$str = $header['c'];
$tagged = substr($str, 0, 2) === '*|' && $pos = strpos($str, '|* ');
if (!$tagged) { continue; }
[$tag, $value] = explode(':', substr($str, 2, $pos - 2));
$header['c'] = substr($str, $pos + 3);
$header[$tag] = $value;
}
You could for example use 2 capturing groups and use those as the values for the c and new class key.
For the second capturing group you could make sure to match at least a single non whitespace char using \S
Note to repeat the character class 1 or more times and you don't have to escape the pipe in the character class.
\*\|class:([^|]+)\|\*\h*(\S.*)
Or if what follows for group 2 can be optional:
\*\|class:([^\|]+)\|\*\h*(.*)
Regex demo
Explanation first pattern
\*\| Match *|
class: Match literally
([^|]+) Capture group 1, match 1+ times any char except |
\|\*\h* Match |* followed by 0+ horizontal whitespace chars
(\S.*) Capture group 2, match a non whitespace char and 0+ times any char except a newline
Regex demo | Php demo
Example code
$array = [
"c" => "*|class:orange|* hello"
];
$pattern = "~\*\|class:([^|]+)\|\*\h*(\S.*)~";
foreach ($array as $key => $string) {
if (preg_match($pattern, $string, $matches)) {
$array[$key] = $matches[2];
$array["class"] = $matches[1];
}
}
print_r($array);
Output
Array
(
[c] => hello
[class] => orange
)
Imagine I have a certain text file like this:-
52 apple, one and teen, 682
How to do I take them like this?
$a['aa']['a1'] - 52
$a['aa']['a2'] - apple
$a['bb']['b1'] - one
$a['bb']['b2'] - and
$a['bb']['b3'] - teen
$a['cc']['c1'] - 682
This reads lines from a file, splits them by comma+space and splits each of those by a space:
<?php
$lines = file($filename);
$foo = array_map(
function($v) {
return array_map(
function($u) {
return explode(" ", $u);
},
explode(", ", trim($v))
);
},
$lines
);
var_dump($foo);
Result:
array(1) {
[0]=>
array(3) {
[0]=>
array(2) {
[0]=>
string(2) "52"
[1]=>
string(5) "apple"
}
[1]=>
array(3) {
[0]=>
string(3) "one"
[1]=>
string(3) "and"
[2]=>
string(4) "teen"
}
[2]=>
array(1) {
[0]=>
string(3) "682"
}
}
}
If you really need specific indices you could handle those in the anonymous functions for each array_map on your own. Right now they're just numerically indexed.
need some help with splitting mysql single column query array into different php variables here.
example:
here's the query, it's pretty simple to be honest.
but, i'm running out of ideas right now.
$string = "select Description from tblQuestion
where Employeeid = '$param'"
$query = $this->db->query($string);
$result = return $query->result_array();
btw, i am using Codeigniter and i tried to var_dump and the results are like this.
array(9) { [0]=> array(1) { ["Description"]=> string(5) "tidak" } [1]=> array(1) { ["Description"]=> string(5) "tidak" } [2]=> array(1) { ["Description"]=> string(5) "tidak" } [3]=> array(1) { ["Description"]=> string(5) "tidak" } [4]=> array(1) { ["Description"]=> string(5) "tidak" } [5]=> array(1) { ["Description"]=> string(5) "tidak" } [6]=> array(1) { ["Description"]=> string(5) "tidak" } [7]=> array(1) { ["Description"]=> string(5) "tidak" } [8]=> array(1) { ["Description"]=> string(5) "tidak" } }
i tried to use json_encode and the result is
[{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"}]
the question is.
how do i convert this stack of arrays into different variables like this?
$var0 = "tidak";
$var1 = "tidak";
$var2 = "tidak";
$var3 = "tidak";
and on and on....
thanks in advance.
cheers!
Put the results in a foreach loop and assign the values to a dynamic variable...
sample code like,
foreach($results as $key=>$val){
$str = 'var'.$key;
$$str = $val['Description'];
}
echo $var0;
Kinda of a noobie in PHP and Regex, I receive the following from a web service:
test:002005#1111#333333#;10205#2000#666666#;002005#1111#55555#;
The above line is a sequence of 3 numbers which repeats 3 times. I would like to get the 3rd number of each sequence and I believe the best course (besides 3000 explodes) would be preg_match_all but I am having a tough time wrapping my mind around RegEx.
The end result should look like this:
Array
(
[0] => 333333
[1] => 666666
[2] => 55555
)
Thanks in advance for any help.
if(preg_match_all('/.*?(?:\d+#){2}(\d+)#;/',$s,$m)) {
print_r($m[1]);
}
http://ideone.com/99M9t
or
You can do it using explode as:
$input = rtrim($input,';');
$temp1 = explode(';',$input);
foreach($temp1 as $val1) {
$temp2 = explode('#',$val1);
$result[] = $temp2[2];
}
print_r($result);
http://ideone.com/VH29g
Use the function explode()
<?php
$pizza = "piece1#piece2#piece3#piece4#piece5#piece6";
$pieces = explode("#", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
?>
I don't remember exactly how the saying goes but...
"You have a problem and decide to use regular expressions... now you have two problems."
Your problem can easily be solved if we assume 'test:' isn't part of the actual string to be parsed.
<?php
$in = '002005#1111#333333#;10205#2000#666666#;002005#1111#55555#;';
function splitGroupsAndGetColumn($input, $groupSeparator, $columnSeparator, $columnIndex, $skipEmpty=true)
{
$result = array();
$groups = explode($groupSeparator, $input);
foreach($groups as $group)
{
$columns = explode($columnSeparator, $group);
if (isset($columns[$columnIndex]))
{
array_push($result, $columns[$columnIndex]);
}
else if (! $skipEmpty)
{
array_push($result, NULL);
}
}
return $result;
}
var_dump(splitGroupsAndGetColumn($in, ';', '#', 2));
Output:
array(3) {
[0]=>
string(6) "333333"
[1]=>
string(6) "666666"
[2]=>
string(5) "55555"
}
You could use preg_match_all for this task, which makes the task quite simple:
$a = "test:002005#1111#333333#;10205#2000#666666#;002005#1111#55555#;";
preg_match_all('/#(\d+)#;/', $a, $m);
print_r($m);
$m[1] contains the output, you want.
Reference: http://php.net/manual/en/function.preg-match-all.php
My version :)
The regex (\d+) means I want all that is a number one or more
php > $a = '002005#1111#333333#;10205#2000#666666#;002005#1111#55555#';
php > preg_match_all('/(\d+)/',$a,$matches);
php > var_dump($matches);
array(2) {
[0]=>
array(9) {
[0]=>
string(6) "002005"
[1]=>
string(4) "1111"
[2]=>
string(6) "333333"
[3]=>
string(5) "10205"
[4]=>
string(4) "2000"
[5]=>
string(6) "666666"
[6]=>
string(6) "002005"
[7]=>
string(4) "1111"
[8]=>
string(5) "55555"
}
[1]=>
array(9) {
[0]=>
string(6) "002005"
[1]=>
string(4) "1111"
[2]=>
string(6) "333333"
[3]=>
string(5) "10205"
[4]=>
string(4) "2000"
[5]=>
string(6) "666666"
[6]=>
string(6) "002005"
[7]=>
string(4) "1111"
[8]=>
string(5) "55555"
}
}
I have a problem with preg_match_all.
While preg_match does reply the whole match as the first element of the array, preg_match_all doesn't - the first array is empty.
At least with the pattern I chose (havn't tried others since it's the one I need) it doesn't work.
Here is my code:
preg_match_all("/<\?\?(\t| )?translate(\t| )?;(\t| )?(.*)(\t| )?\?\?>/U", $file, $translate_info);
The pattern itself is working and producing subpattern matches.
Updated according to new given details :
$file = '<?? translate ; foo bar??>';
$res = preg_match_all('/<\?\?(\t| )?translate(\t| )?;(\t| )?(.*)(\t| )?\?\?>/U', $file, $translate_info);
echo "res='$res'\n";
var_dump($translate_info);
Works for me, it gives :
res='1'
array(6) {
[0]=>
array(1) {
[0]=>
string(26) "<?? translate ; foo bar??>"
}
[1]=>
array(1) {
[0]=>
string(1) " "
}
[2]=>
array(1) {
[0]=>
string(1) " "
}
[3]=>
array(1) {
[0]=>
string(0) ""
}
[4]=>
array(1) {
[0]=>
string(8) " foo bar"
}
[5]=>
array(1) {
[0]=>
string(0) ""
}
}