Get text that is within brackets with single or double quotes - php

I try to found in my all PHP files the strings inside the i18n functions. Here is an example:
$string = '__("String 2"); __("String 3", __("String 4"));' . "__('String 5'); __('String 6', __('String 7'));";
var_dump(preg_match_all('#__\((\'|")([^\'"]+)(\'|")\)#', $string, $match));
var_dump($match);
I wanna get this result:
array
0 => array
0 => string 'String 2' (length=8)
1 => string 'String 3' (length=8)
2 => string 'String 4' (length=8)
3 => string 'String 5' (length=8)
4 => string 'String 6' (length=8)
4 => string 'String 7' (length=8)
But unfortunately I get this result
array
0 => array
0 => string '__("esto es una prueba")' (length=24)
1 => string '__("esto es una prueba 2")' (length=26)
2 => string '__("prueba 4")' (length=14)
3 => string '__('caca')' (length=10)
4 => string '__('asdsnasdad')' (length=16)
1 => array
0 => string '"' (length=1)
1 => string '"' (length=1)
2 => string '"' (length=1)
3 => string ''' (length=1)
4 => string ''' (length=1)
2 => array
0 => string 'esto es una prueba' (length=18)
1 => string 'esto es una prueba 2' (length=20)
2 => string 'prueba 4' (length=8)
3 => string 'caca' (length=4)
4 => string 'asdsnasdad' (length=10)
3 => array
0 => string '"' (length=1)
1 => string '"' (length=1)
2 => string '"' (length=1)
3 => string ''' (length=1)
4 => string ''' (length=1)
Thanks in advance.

preg_match_all('/(?<=\(["\']).*?(?=[\'"])/', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];
Simple.
Note that I am using ( as an entry point to the match. If you have more exotic input you should provide it.

Don't capture the quotes.
preg_match_all('#__\([\'"]([^\'"]+)[\'"]\)#', $string, $match);
Also take a look at the flags parameter for preg_match_all() for different output formats.

Related

PHP str_split on string with decoded html_entity

If I run this code:
<?php
$string = 'My string ‘to parse’';
$string_decoded = html_entity_decode($string, ENT_QUOTES, 'utf-8');
$string_array = str_split($string_decoded);
var_dump($string_array);
?>
I get this result:
array (size=28)
0 => string 'M' (length=1)
1 => string 'y' (length=1)
2 => string ' ' (length=1)
3 => string 's' (length=1)
4 => string 't' (length=1)
5 => string 'r' (length=1)
6 => string 'i' (length=1)
7 => string 'n' (length=1)
8 => string 'g' (length=1)
9 => string ' ' (length=1)
10 => string '�' (length=1)
11 => string '�' (length=1)
12 => string '�' (length=1)
13 => string 't' (length=1)
14 => string 'o' (length=1)
15 => string ' ' (length=1)
16 => string 'p' (length=1)
17 => string 'a' (length=1)
18 => string 'r' (length=1)
19 => string 's' (length=1)
20 => string 'e' (length=1)
21 => string '�' (length=1)
22 => string '�' (length=1)
23 => string '�' (length=1)
As you can see, instead of the decoded single quotes (left/right), I'm getting these three characters for each quote...
I noticed that this happens with some entities, but not others. A few that present this issue are ‘ ” $copy;. Some that don't present the same problem are & $gt;.
I tried different charsets but couldn't find one that would work for all.
What am I doing wrong? Is there a way to make it work for all entities? Or at least all the "common" ones?
Thanks.
This should do well:
function mb_str_split($string) {
return preg_split('/(?<!^)(?!$)/u', $string );
}
$string = 'My string ‘to parse’';
$string = utf8_encode($string);
$string_decoded = html_entity_decode($string, ENT_QUOTES, 'utf-8');
$string_array = mb_str_split($string_decoded);
var_dump($string_array);
As mentioned in comments: you need to split the string with mb_split or by regex.
Proof: https://3v4l.org/3FRmG

Looping over Joined query

I've got a query which outputs the following data:
Treatment ID --------- Treatment_Name ---------- Customer_type
1 Treatment 1 Ladies
1 Treatment 1 Mens
3 Treatment 2 Ladies
4 Treatment 3 Mens
I need to loop over this and output a similar structure to this:
<div class="mens ladies"> <!-- these are read from the customer_type column -->
<h4>Treatment 1</h4> <!-- this is read from the treatment_name column -->
</div>
So basically grouping by the Treatment name (or ID) and then outputting all of the customers related to this treatment.
Coming from a ColdFusion background I assumed this would be easy with a Query of Queries etc. however I've struggled with coming up with a PHP solution.
I've tried to do a while loop and only show data if the treatment_name has changed which seems to work fine but I'm not sure how to display the customer_types related to this.
Any ideas? Thanks.
Edit
Here is a copy of the query returned from the db using a mysqli_fetch_all:
array (size=22)
0 =>
array (size=3)
0 => string 'Mens' (length=4)
1 => string '1' (length=1)
2 => string 'treatment 1' (length=11)
1 =>
array (size=3)
0 => string 'Ladies' (length=6)
1 => string '1' (length=1)
2 => string 'treatment 1' (length=11)
2 =>
array (size=3)
0 => string 'Kids' (length=4)
1 => string '2' (length=1)
2 => string 'treatment 2' (length=11)
3 =>
array (size=3)
0 => string 'Ladies' (length=6)
1 => string '2' (length=1)
2 => string 'treatment 2' (length=11)
4 =>
array (size=3)
0 => string 'Mens' (length=4)
1 => string '2' (length=1)
2 => string 'treatment 2' (length=11)
5 =>
array (size=3)
0 => string 'Mens' (length=4)
1 => string '3' (length=1)
2 => string 'treatment 3' (length=11)
6 =>
array (size=3)
0 => string 'Kids' (length=4)
1 => string '3' (length=1)
2 => string 'treatment 3' (length=11)
7 =>
array (size=3)
0 => string 'Ladies' (length=6)
1 => string '3' (length=1)
2 => string 'treatment 3' (length=11)
8 =>
array (size=3)
0 => string 'Ladies' (length=6)
1 => string '4' (length=1)
2 => string 'treatment 4' (length=11)
It continues in the same format...
Hope this answers your question:
<?php
$link = mysqli_connect("hostname","username","password","database","port") or die("Error " . mysqli_error($link));
$query = "SELECT treatment_name,Customer_type from Treatments" or die("Error at.." . mysqli_error($link));
$result = $link->query($query);
while($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
//I have used this sample array: Try and make your result set this way
$data = array(
array('treatment_name'=>'Treatment1','Customer_type'=>'Mens'),
array('treatment_name'=>'Treatment1','Customer_type'=>'Ladies'),
array('treatment_name'=>'Treatment2','Customer_type'=>'Mens')
);
foreach ($data as $key => &$value) {
$temp[$value['treatment_name']][$key] = $value['Customer_type'];
}
//echo "<pre>";
//print_r($data);
//print_r($temp);
//die;
foreach($temp as $key => $value){
echo "<div class = \"";
foreach($value as $key2 => $value2)
echo " ". $value2. " ";
echo "\"><h4>".$key."</h4></div></br>";
}
?>
//Output
<div class = " Mens Ladies "><h4>Treatment1</h4></div></br><div class = " Mens "><h4>Treatment2</h4></div></br>
You can use the commented lines for checking the data and adjusting accordingly.
By the way, this code directly prints the output as html, so your browser will render it. Right click the page and view source if you want the content!

How to group array but not create new object?

I have an array that looks like this:
array (size=21)
0 => string '2' (length=1)
1 => string '' (length=0)
2 => string '20' (length=2)
3 => string '19' (length=2)
4 => string '14' (length=2)
5 => string '13' (length=2)
6 => string '' (length=0)
7 => null
8 => null
9 => string '20' (length=2)
10 => null
11 => string '10' (length=2)
12 => string '' (length=0)
13 => null
14 => string '13' (length=2)
15 => null
16 => string '' (length=0)
17 => null
18 => null
19 => string '' (length=0)
20 => string '20' (length=2)
And I would like to create a new array from this array by grouping rows with the same string. e.g.
2 => string '20' (length=2) with 20 => string '20' (length=2) and with 9 => string '20' (length=2)
and
5 => string '13' (length=2) with 5 => string '13' (length=2)
etc.
and order the new created array rows based on how many times the string occures there.
Order need to be DESC from the most occurrences to the last like a classic top something chart (The most present strings are first and the least are low)
So, the modified array will look like this:
array (size=21)
0 => string '20' (length=2)
1 => string '13' (length=2)
...
I also need somehow to handle null results e.g. 17 => null to be not incorporated at all in the final array modified result.
This should do the trick:
// Filter the "null results" first
$myarray = array_filter($myarray, create_function('$arg', '
return !is_null($arg);
'));
$occurrences = array_count_values($myarray);
// EDIT: arsort preserves the key => value correlation
arsort($occurrences, SORT_NUMERIC);
var_dump(array_keys($occurrences));
Try this.
$result = array_count_values($a);
arsort($result);
$result = array_keys($result);

How to filter a dirty array into a clean stream of data

I have here what I would call a dirty array,
this dirty array needs to be filtered so it is a clean array e.g.
Below is the Array.
array
0 => string '1' (length=1)
1 => string 'FIRSTNAME A' (length=7)
2 => string 'LASTNAME B' (length=10)
3 => string '2011-12-08 16:15:37' (length=19)
4 => string '2' (length=1)
5 => string 'FIRSTNAME B' (length=7)
6 => string 'LASTNAME B' (length=10)
7 => string '2011-12-08 16:15:43' (length=19)
8 => string '3' (length=1)
9 => string 'FIRSTNAME C' (length=7)
10 => string 'LASTNAME C' (length=10)
11 => string '2011-12-08 16:15:48' (length=19)
12 => string '4' (length=1)
13 => string 'FIRSTNAME D' (length=7)
14 => string 'LASTNAME D' (length=10)
15 => string '2011-12-08 16:15:55' (length=19)
16 => string '6' (length=1)
17 => string 'FIRSTNAME E' (length=7)
18 => string 'LASTNAME E' (length=10)
19 => string '2011-12-08 16:16:08' (length=19)
I want the final output to look like
array[0]= 1, FIRSTNAME A, LASTNAME A, DATE
array[1]= 2, FIRSTNAME B, LASTNAME B, DATE
array[2]= 3, FIRSTNAME C, LASTNAME C, DATE
array[3]= 4, FIRSTNAME D, LASTNAME D, DATE
array[4]= 4, FIRSTNAME E, LASTNAME E, DATE
This should work
$clean = array_chunk($dirty, 4);
more about array_chunk
Wow, I'm going to take a stab at this. Not completely sure what your asking, but hopefully this will get us in some direction:
$cleanArray = array_chunk($dirtyArray,4);
foreach($cleanArray as $value) {
$finalArray[] = implode(", ",$value);
}
print_r($finalArray);

Generate table with merged cells

I need some help with tables.
I've got some example data:
array
0 =>
array
'ID' => string '1' (length=1)
0 => string '1' (length=1)
'Name' => string 'Jon' (length=3)
1 => string 'Jon' (length=3)
'SecondName' => string 'Dee' (length=3)
2 => string 'Dee' (length=3)
'Date' => string '3, 2011' (length=7)
3 => string '3, 2011' (length=7)
'TotalHours' => string '06:48' (length=5)
4 => string '06:48' (length=5)
'TotalFee' => string '124' (length=3)
5 => string '124' (length=3)
1 =>
array
'ID' => string '3' (length=1)
0 => string '3' (length=1)
'Name' => string 'Some ' (length=5)
1 => string 'Some ' (length=5)
'SecondName' => string 'Preson' (length=6)
2 => string 'Preson' (length=6)
'Date' => string '3, 2011' (length=7)
3 => string '3, 2011' (length=7)
'TotalHours' => string '32:06' (length=5)
4 => string '32:06' (length=5)
'TotalFee' => string '436' (length=3)
5 => string '436' (length=3)
2 =>
array
'ID' => string '3' (length=1)
0 => string '3' (length=1)
'Name' => string 'Some ' (length=5)
1 => string 'Some ' (length=5)
'SecondName' => string 'Preson' (length=6)
2 => string 'Preson' (length=6)
'Date' => string '4, 2011' (length=7)
3 => string '4, 2011' (length=7)
'TotalHours' => string '10:00' (length=5)
4 => string '10:00' (length=5)
'TotalFee' => string '1345' (length=4)
5 => string '1345' (length=4)
Now I'm trying to dynamically create html table with this data grouped by person, so Name/Second name wont repeat in every row. Is it possible to merge Name cells?
/-----------------------------------------\
| | month 1 | hours | cash |
| Person 1 |------------------------------|
| | month 2 | hours | cash |
|-----------------------------------------|
| Person 2 | month 1 | hours | cash |
\-----------------------------------------/
etc...
Is it possible?
The thing you are looking for is a rowspan.
Check out this link: http://www.htmlcodetutorial.com/tables/index_famsupp_30.html
Usage:
<TABLE BORDER=2 CELLPADDING=4>
<TR>
<TH ROWSPAN=3 BGCOLOR="#99CCFF">Production</TH>
<TD>Raha Mutisya</TD> <TD>1493</TD>
</TR>
<TR>
<TD>Shalom Buraka</TD> <TD>3829</TD>
</TR>
<TR>
<TD>Brandy Davis</TD> <TD>0283</TD>
</TR>
<TR>
<TH ROWSPAN=3 BGCOLOR="#99CCFF">Sales</TH>
<TD>Claire Horne</TD> <TD>4827</TD>
</TR>
<TR>
<TD>Bruce Eckel</TD> <TD>7246</TD>
</TR>
<TR>
<TD>Danny Zeman</TD> <TD>5689</TD>
</TR>
</TABLE>
</TABLE></html>
You need to add this properly ofcourse, but it gives you the basic idea.
Also you need to order the results by the person's name (or ID) and keep track if it changes when running through the resultset. That's the easiest solution.

Categories