0

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); 
} 

    ?>
9
  • You want just to compare two texes? Commented Jan 20, 2021 at 7:03
  • yes, to make sure both are these variables have the same characters Commented Jan 20, 2021 at 7:04
  • ok sir hold on, its abit more complicated than that, i apologize for not elaborating Commented Jan 20, 2021 at 7:08
  • So, im doing a search box, so the 1st variable will be the text entered and it will pass it over to the result page where it will compare it with the 2nd variable. Commented Jan 20, 2021 at 7:09
  • @azibom will take a look in abit, thanks Commented Jan 20, 2021 at 7:51

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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!";       
}

3 Comments

so the only way is to explode the dashes so it can validate?
what is the problem with this solution?
my 2nd variable which is $string2 will be reading an array of these strings from a txt.file. so my search box text which is $string1 will have to compare with all the other strings in my txt.file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.