Reputation: 459
I'm not sure if alphanumeric is the correct term but I would like to validate characters that are in a combination of strings, integers and dashes. Example: yui-4444-99
. So far, i've tried intval, strcmp, ctype_alnum and preg_match but it doesnt really work.
sample:
<?php
if (ctype_alnum($string1) == ctype_alnum($string2)) {
echo "nice!";
} else {
}
my txt.file
abc-1234-99
Cedric
93482812
[email protected]
---------------------------------------------
def-4332-99
Wendy
98238432
[email protected]
Guitar
2010
Yamaha
Scratches on the side
Used
---------------------------------------------
fgh-4567-99
Wendy
98238432
[email protected]
---------------------------------------------
yui-4444-99
Wendy
98238432
[email protected]
---------------------------------------------
vbn-5624-99
jason
98238432
[email protected]
---------------------------------------------
This is how i extracted my values to diff variables.
<?php
$handle = @fopen('listings.txt', "r");
$row = 0;
$count = 0;
$line0 = [];
$line1 = [];
$line2 = [];
$line3 = [];
$line4 = [];
$line5 = [];
$line6 = [];
$line7 = [];
$line8 = [];
$line9 = [];
if ($handle) {
while (!feof($handle)) {
$store = fgets($handle, 4096);
if ($row == 10){
$row = 0;
$count++;
}
if ($row == 0)
{
$line0[] = strval($store);
}
else if($row == 1) {
$line1[] = strval($store);}
else if($row == 2) {
$line2[] = strval($store);}
else if($row == 3) {
$line3[] = strval($store);}
else if($row == 4) {
$line4[] = strval($store);}
else if($row == 5) {
$line5[] = strval($store);}
else if($row == 6) {
$line6[] = strval($store);}
else if($row == 7) {
$line7[] = strval($store);}
else if($row == 8) {
$line8[] = strval($store);}
$row++;
}
$sn = 0;
if (isset($_GET['sn'])) {
$sn=$_GET['sn'];
}
$item = count($line0);
for ($c=0; $c<$item; $c++)
{
if((intval($line0[$c]) == intval($sn))) {
echo $line0[$c],"<br>";
echo $line1[$c],"<br>";
echo $line2[$c],"<br>";
echo $line3[$c],"<br>";
echo $line4[$c],"<br>";
echo $line5[$c],"<br>";
echo $line6[$c],"<br>";
echo $line7[$c],"<br>";
echo $line8[$c],"<br>";
break;
}
}
?>
<?php
fclose($handle);
}
?>
Upvotes: 0
Views: 144
Reputation: 72269
You need to shorten out code of reading file using file()
.
Also needs to rectify comparison code as well.
Do like below:
<?php
$string2 = file('listings.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$foundArray = [];
foreach($string2 as $string){
if (ctype_alnum($string1) == ctype_alnum($string)) {
$foundArray[] = $string;
}
}
echo "found matches :".print_r($foundArray,true);
Sample output: https://3v4l.org/Vt4OE
Upvotes: 1
Reputation: 1944
If you want to just compare the first part of them, you can use this code:
<?php
$string1 = "sdf-98-s";
$stringArray = ["sdf", "asd"];
$myArray = explode('-', $string1); // make the array from string
$firstElement = reset($myArray); // choose the first element
if (in_array($firstElement, $stringArray)) {
echo "Find it!";
} else {
echo "Fail to find it!";
}
Upvotes: 1
Reputation: 2968
Starting from PHP 8, you can compare strings to their numeric equivalent:
$test = ['', 'a23', '23', 'abc'];
foreach ($test as $string) {
echo ($string != (int) $string ? 'TRUE' : 'FALSE') . PHP_EOL;
}
Output:
TRUE
TRUE
FALSE
TRUE
Upvotes: 2