I have an array with some urls. I want to echo the url which contains the $string. for the moment I only echo "match found". here is the code :
`$string="test";
$people = array(domain.com/test-2.html, "domain.com/2.html", "Glenn", "domain.com/3.html");
if (in_array($string, $people))
{echo "Match found";}
else {echo "Match not found";}`
thanks
So actually you misunderstood how in_array works when you do :
$string="test";
$people = array("domain.com/test-2.html", "domain.com/2.html", "Glenn", "domain.com/3.html");
if (in_array($string, $people)) {
echo "Match found";
}
else {
echo "Match not found";
}
It should always echo "Match not found" if $string is not exactly equals to one of the people value (e.g. $string = "domain.com/test-2.html").
If you want to find a subset of string ("test") in an array the easiest way is to itarate through the values and comparing with strpos :
$string="test";
$people = array("domain.com/test-2.html", "domain.com/2.html", "Glenn", "domain.com/3.html");
$found = false;
foreach ($people as $url) {
if (strpos($url, $string)) {
echo "Match found : " . $url; # Or just : echo $url;
$found = true;
}
}
if (!$found) {
echo "Match not found";
}
Example testable here : http://sandbox.onlinephpfunctions.com/code/b0c39c9493c5731a847d36c4b3ed85686cc21c6b
Related
i have this piece of code. On the top i download text data from URL and i get string. This string has some uninportant text so this text is in $info and important number are in $array than i did some reIndex array because for some reason i had $array[0] empty.
I have 2 array: $array and $array_try both arrays shoud have similar structure as u can see here:
For some reason im getting Undefined array key 1 in this line $array_value[] = $parts[1];
When i change in foreach loop $array for $array_try it works well.
Best
$data = file_get_contents("$url");
$data = explode('Kurz', $data);
$kurz = '|Kurz';
$info= substr_replace($data[0], $kurz, -1, );
echo "<br>";
$array = explode ("\n", $data[1]);
//foreach ($array as $value){
// echo $value . "<br>";
//}
unset($array[0]);
$array = array_values($array);
$array_try = [
"04.01.2021|26,140",
"05.01.2021|26,225",
"06.01.2021|26,145"
];
$array_date = [];
$array_value = [];
foreach($array as $value) {
$parts = explode('|', $value);
$array_date[] = $parts[0];
$array_value[] = $parts[1];
}
var_dump($array_try);
echo "<br>";
echo "<br>";
var_dump($array);
I have written the below to error trap an empty array and it isn't working. Any ideas on the syntax I need?
$inputstring = "MyOtherFile.rdl" "MyFile.rdl"
$cleanstring = $inputstring.replace(""" """,";")
$filearray = $inputstring.split(";")
if (echo #($filearray).length = "0")
{$filearray.length
'No Files Selected'
exit}
else
{$filearray.length}
It is returning 2 for the array length but is still tripping the 1st part of the IF and saying no files selected.
You could do something like this:
function ValidateArrayLength([string[]] $files) {
if ($files.length -eq 0) {
$files.length
'No Files Selected'
exit
}
else {
$files.length
}
}
$filearray = #("MyOtherFile.rdl", "MyFile.rdl")
ValidateArrayLength -files $filearray
$filearray = #()
ValidateArrayLength -files $filearray
I have array of IDs. I have one ID which I want to find if that ID exists in the array of IDs in Perl
I tried the following code:
my $ids = [7,8,9];
my $id = 9;
foreach my $new_id (#$ids) {
if ($new_id == $id) {
print 'yes';
} else {
print 'no';
}
}
I get the output as:
nonoyes
Instead I want to get the output as only:
yes
Since ID exists in array of IDs
Can anyone please help ?
Thanks in advance
my $ids = [7,8,9];
my $id = 9;
if (grep $_ == $id, #ids) {
print $id. " is in the array of ids";
} else {
print $id. " is NOT in the array";
}
You just need to remove the else part and break the loop on finding the match:
my $flag = 0;
foreach my $new_id (#$ids) {
if ($new_id == $id) {
print 'yes';
$flag = 1;
last;
}
}
if ($flag == 0){
print "no";
}
Another option using hash:
my %hash = map { $_ => 1 } #$ids;
if (exists($hash{$id})){
print "yes";
}else{
print "no";
}
use List::Util qw(any); # core module
my $id = 9;
my $ids = [7,8,9];
my $found_it = any { $_ == $id } #$ids;
print "yes" if $found_it;
The following piece of code should cover your requirements
use strict;
use warnings;
my $ids = [7,8,9];
my $id = 9;
my $flag = 0;
map{ $flag = 1 if $_ == $id } #$ids;
print $flag ? 'yes' : 'no';
NOTE: perhaps my #ids = [7,8,9]; is better way to assign an array to variable
I have this code:
$str='ياسمين';
$a=utf8Split(trim($str));
for($i=0;$i<=strlen($a);$i++)
{
$sql_char=("SELECT num FROM alphabet where letters = '$a[$i]'");
$result = mysql_query ($sql_char,$dbConn);
while($row = mysql_fetch_array($result))
echo $row['num'].'<br>';
}
Try simply to add your $row['num'] to a new variable
$sum=0;
while($row = mysql_fetch_array($result)){
echo $row['num'].'<br>';
$sum=$sum+$row['num'];
}
echo $sum; //171
EDIT
function mb_str_split( $string ) {
# Split at all position not after the start: ^
# and not before the end: $
return preg_split('/(?<!^)(?!$)/u', $string );
}
$str = 'ياسمين';
$charlist = mb_str_split( $str );
print_r( $charlist );
What I'm trying to achieve:
Show either the post tag (without the link) or the title.
If a tag exists, that gets printed if it doesn't, the title gets used.
Issue: I've used get_the_tags() as suggested in the Codex to get the tag without the link and that works, yet it gets the word "Array" printed as a preffix, too.
<?php
if( has_tag() )
{
echo $posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ' ';
}
}
}
else { echo the_title(); };
?>
What am I missing?
You are echo ing $posttags which is an array. If you echo an array it will echo array as output
<?php
if( has_tag() )
{
This is printing Array as prefix ----> echo $posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ' ';
}
}
}
else { echo the_title(); };
?>
Please remove that echo , so your new code will be
<?php
if( has_tag() )
{
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ' ';
}
}
}
else { echo the_title(); };
?>
Hope this helps you