[ {"play":"hdr.1","name":"1","year":"1994","class":"act bio deo"},
{"play":"hdr.2","name":"2","year":"1972","class":"deo bio sil"},
{"play":"hdr.3","name":"3","year":"1974","class":"sil moc tel"},
{"play":"hdr.5","name":"4","year":"1994","class":"rep sim fal"},
{"play":"hdr.6","name":"5","year":"1967","class":"viz tel moc"},
{"play":"hdr.7","name":"6","year":"2003","class":"fal deo dec"},
{"play":"hdr.8","name":"7","year":"1999","class":"tel act bio"},
{"play":"hdr.9","name":"8","year":"1993","class":"mio moc viz"},
{"play":"hdr.10","name":"9","year":"1957","class":"fal dec mio"} ]
I have this json.json data and i use this php code to make from all links that i use somewhere:
<?php
$fill = file_get_contents("json.json");
$tstJson = json_decode($fill);
foreach($tstJson as $val)
echo "<a class='".$val->class."' href='?".$val->play."' >".$val->name."</a>";
?>
But how do i make this to echo only the classes that contains "act" ?
Like a sorting mode.
You can use strpos() to check if 'act' is in class before echoing:
foreach($tstJson as $val)
if (strpos($val->class, 'act') !== false)
echo "<a class='".$val->class."' href='?".$val->play."' >".$val->name."</a>";
See demo
You just need an if with a preg_match:
<?php
$fill = file_get_contents("json.json");
$tstJson = json_decode($fill);
foreach ($tstJson as $val) {
if (preg_match('/\bact\b/', $val->class)) {
echo "<a class='".$val->class."' href='?".$val->play."' >".$val->name."</a>";
}
}
?>
The \bact\b checks that the entire word is act (so, for example, fact won't match).
Output (with line breaks for readability):
<a class='act bio deo' href='?hdr.1' >1</a>
<a class='tel act bio' href='?hdr.8' >7</a>
try this
<?php
$fill = file_get_contents("json.json");
$tstJson = json_decode($fill);
foreach($tstJson as $val)
{
$act_class = explode(" ", $val->class);
if(in_array("act", $act_class))
{
echo "<a class='".$val->class."' href='?".$val->play."' >".$val->name."</a>";
}
}
?>
Related
I already have a code:
<?
$eventname = $event->title;
$bad_words = array('Example1','Example2','Example3','Example4','Example5','Example6');
foreach($bad_words as $bad_word){
if(strpos($eventname, $bad_word) !== false) {
echo '';
break;
}
else {
echo '<div class="uk-text-contrast">'.translate('HIDDEN_INFO_DATE').'</div>';
break;
}
}
?>
this works, but only if the $eventname contains example1. But I want to hide echo '<div class="uk-text-contrast">'.translate('HIDDEN_INFO_DATE').'</div>'; from all of the defined words in the $bad_words.
How can I hide the echo if there is one of the $bad_words?
Why don't you use the in_array function? It's even shorter in that way.
if (in_array($eventname, $bad_words)) {
echo '';
} else {
echo '<div>';
}
remobe 'break' from code
foreach($bad_words as $bad_word){
if(strpos($eventname, $bad_word) !== false) {
echo '';
}
else {
echo '<div class="uk-text-contrast">'.translate('HIDDEN_INFO_DATE').'</div>';
}
}
I would convert the string $eventname to an array and then use the array Intersect function
$eventname = "sgswfdg Example1 sdfh dh dfa hadhadh";
$title_to_array=explode(" ", $eventname);
$bad_words = array('Example1','Example2','Example3','Example4','Example5','Example6');
$result = !empty(array_intersect($bad_words , $title_to_array ));
if ($result){
echo '';
} else {
echo $eventname;
}
Rather than using a loop, you could split the title into it's separate words and then check if there are any words in the $bad_words list using array_intersect()
// $eventname = $event->title;
$eventname = 'Some text';
$eventname = 'Some text Example1';
$bad_words = array('Example1','Example2','Example3','Example4','Example5','Example6');
$eventWords = explode(" ", $eventname);
if ( empty (array_intersect($eventWords, $bad_words))) {
// Event name is OK
echo $eventname;
}
This has some examples to show how it works.
Note that this code doesn't pick up things like Example1234 which strpos() would.
Update:
Just to make it more flexible, you can use regular expressions in case you have punctuation in the title...
$eventname = 'Some text';
$eventname = 'Some text,Example1';
$bad_words = array('Example1','Example2','Example3','Example4','Example5','Example6');
preg_match_all('/(\w+)/', $eventname, $eventWords);
if ( empty (array_intersect($eventWords[1], $bad_words))) {
echo '<div class="uk-text-contrast">'.translate('HIDDEN_INFO_DATE').'</div>';
}
( Note: Regular expressions aren't my subject so please let me know if this needs to be improved).
I have the following PHP script that extracts the last 5 rows from file X.
<?php
$f=file("x.txt");
$last=array_slice($f, -5);
echo implode("<br>",$last);
?>
The output is the following:
John
Christmas
George
Luck
Sun
Question: How can I make the output to be clickable links? Example of output I want:
John
Christmas
George
Luck
Sun
I tried something like:
echo implode("<br> <a href='http://google.com/q?' . $last . ''>");
But it didn't work at all... any solutions? Thanks!
Instead of implode(), you can just loop thru $last then echo desired html line
foreach ($last as $value) {
echo "<a href='http://www.google.com?q=$value'>$value</a><br>";
}
Try this :-
<?php
$names = array('John','Christmas','George','Luck','Sun');
foreach($names as $name) {
echo "<br>$name";
}
?>
Happy Coding :-)
I think no need to use implode function.
I have modified your code as below. Hope this will help.
<?php
$f=file("x.txt");
$last=array_slice($f, -5);
if(!empty($last)){
foreach ($last as $value) {
?>
<a href='http://www.google.com?q=<?php echo $value;?>'><?php echo $value;?></a><br>
<?php
}
}
?>
<?php
$f = array("Volvo", "BMW", "Toyota"); //$f=file("x.txt");
$last=array_slice($f, -5);
echo implode("<br>",$last);
foreach ($last as $value) {
echo "<a href='http://www.google.com?q=$value'>$value</a> , ";
}
?>
You can use array_map and implode if you don't want to loop.
$last = array('John','Christmas','George','Luck','Sun');
function addlink($last)
{
return('' . $last .'');
}
$last = array_map("addlink",$last);
echo implode("\n", $last);
https://3v4l.org/l3nME
need to extract an info from a string which strats at 'type-' and ends at '-id'
IDlocationTagID-type-area-id-492
here is the string, so I need to extract values : area and 492 from the string :
After 'type-' and before '-id' and after 'id-'
You can use the preg_match:
For example:
preg_match("/type-(.\w+)-id-(.\d+)/", $input_line, $output_array);
To check, you may need the service:
http://www.phpliveregex.com/
P.S. If the function preg_match will be too heavy, there is an alternative solution:
$str = 'IDlocationTagID-type-area-id-492';
$itr = new ArrayIterator(explode('-', $str));
foreach($itr as $key => $value) {
if($value === 'type') {
$itr->next();
var_dump($itr->current());
}
if($value === 'id') {
$itr->next();
var_dump($itr->current());
}
}
This is what you want using two explode.
$str = 'IDlocationTagID-type-area-id-492';
echo explode("-id", explode("type-", $str)[1])[0]; //area
echo trim(explode("-id", explode("type-", $str)[1])[1], '-'); //492
Little Simple ways.
echo explode("type-", explode("-id-", $str)[0])[1]; // area
echo explode("-id-", $str)[1]; // 492
Using Regular Expression:
preg_match("/type-(.*)-id-(.*)/", $str, $output_array);
print_r($output_array);
echo $area = $output_array[1]; // area
echo $fnt = $output_array[2]; // 492
You can use explode to get the values:
$a = "IDlocationTagID-type-area-id-492";
$data = explode("-",$a);
echo "Area ".$data[2]." Id ".$data[4];
$matches = null;
$returnValue = preg_match('/type-(.*?)-id/', $yourString, $matches);
echo($matches[1]);
I have a table with data from sql. Some of the columns in the db have more than 1 name. I have created an array from them but now I need to compare the two while creating a table.
$strArrayChildren = explode(',', $children);
$strArrayChildren = str_replace('and', '', $strArrayChildren);
$childCount = count($strArrayChildren);
$strArrayGrades = explode(',',$the_grades);
$strArrayGrades = str_replace('(', '', $strArrayGrades);
$strArrayGrades = str_replace(')', '', $strArrayGrades);
$grades ='';
foreach($strArrayChildren as $child){
foreach($strArrayGrades as $grade){
if(strpos($grade, $child) !== false){
$grades = preg_replace('([a-z A-Z ()]+)', "", $grade);
}elseif(strpos($grade, $child) !== true){
$grades ='';
}
}
echo "<tr>";
echo "<td>{$child}</td>";
echo "<td>{$last_name}</td>";
echo "<td>Child</td>";
echo "<td>{$grades}</td>";
echo "</tr>";
}
When I run this code I get the grade of the student to match with the first name from the array, but then the rest of the grades keep trying to match with the first student even though there is a new row with a new name.
Any help would be great! Thank you!
I'm not sure about your database but this should work. I'm posting the response with static arrays.
<?php
$strArrayChildren = array("Becky"," Aaron"," Luke");
$the_grades = "9 (Susan), 5 (Luke)";
$strArrayGrades = explode(',',$the_grades);
echo "<html><body><table style='text-align: center; width: 400px;'>";
echo "<tr>";
echo "<td>Child</td>";
echo "<td>Grade</td>";
echo "</tr>";
foreach($strArrayChildren as $child){
$grades = "";
$child = trim($child);
foreach($strArrayGrades as $key => $grade){
if(strpos($grade, $child) > 0){
$grades = intval(preg_replace('/[^0-9]+/', '', $grade), 10);
}
}
echo "<tr>";
echo "<td>{$child}</td>";
echo "<td>{$grades}</td>";
echo "</tr>";
}
echo "</table></body></html>";
?>
Explanation:
you need to initialize $grades right after first loop start
There is no point to test both states of strpos() function
It's safe to check position of needle occurrence in the string (be grater than 0)
You need to change you regex for selecting numbers from an string.
Trim needle in strpos(); there are unwanted white space in some cases. Better to trim white spaces at the start of the loop
I have these php lines:
<?php
$start_text = '<username="';
$end_text = '" userid=';
$source = file_get_contents('http://mysites/users.xml');
$start_pos = strpos($source, $start_text) + strlen($start_text);
$end_pos = strpos($source, $end_text) - $start_pos;
$found_text = substr($source, $start_pos, $end_pos);
echo $found_text;
?>
I want to see just the names from entire file, but it shows me just the first name. I want to see all names.
I think it is something like: foreach ($found_text as $username).... but here I am stuck.
Update from OP post, below:
<?php
$xml = simplexml_load_file("users.xml");
foreach ($xml->children() as $child)
{
foreach($child->attributes() as $a => $b)
{
echo $a,'="',$b,"\"</br>";
}
foreach ($child->children() as $child2)
{
foreach($child2->attributes() as $c => $d)
{
echo "<font color='red'>".$c,'="',$d,"\"</font></br>";
}
}
}
?>
with this code, i receive all details about my users, but from all these details i want to see just 2 or 3
Now i see :
name="xxx"
type="default"
can_accept="true"
can_cancel="false"
image="avatars/trophy.png"
title="starter"
........etc
Another details from the same user "Red color(defined on script)"
reward_value="200"
reward_qty="1"
expiration_date="12/07/2012"
.....etc
what i want to see?
i.e first line from first column "name="xxx" & expiration_date="12/07/2012" from second column
You will need to repeat the loop, using the 3rd parameter, offset, of the strpos function. That way, you can look for a new name each time.
Something like this (untested)
<?php
$start_text = '<username="';
$end_text = '" userid=';
$source = file_get_contents('http://mysites/users.xml');
$offset = 0;
while (false !== ($start_pos = strpos($source, $start_text, $offset)))
{
$start_pos += strlen($start_text);
$end_pos = strpos($source, $end_text, $offset);
$offset = $end_pos;
$text_length = $end_pos - $start_pos;
$found_text = substr($source, $start_pos, $text_length);
echo $found_text;
}
?>
You should either use XMLReader or DOM or SimpleXML to read XML files. If you don't see the necessity, try the following regular expressions approach to retrieve all usernames:
<?php
$xml = '<xml><username="hello" userid="123" /> <something /> <username="foobar" userid="333" /></xml>';
if (preg_match_all('#<username="(?<name>[^"]+)"#', $xml, $matches, PREG_PATTERN_ORDER)) {
var_dump($matches['name']);
} else {
echo 'no <username="" found';
}