pjustindaryll
pjustindaryll

Reputation: 377

Clarification on preg_match() | Will preg_match() accept data from SQL Query result?

I have a question regarding preg_match(), does it accept string from a query?

Does this work?

$founder = $founder['idno']; -> ex. $founder has a string value of AAA111

Will this work like this?

Supposing $founder['idno'] = AAA111

<?php 
$founder = $founder['idno'];
preg_match("/(\\d+)([a-zA-Z]+)/", $founder, $result);
print($result[1]);
print($result[2]); ?>

It's showing an error

Notice: Undefined offset: 1 in C:\xampp\htdocs\address\to\file\founder_data_modal.php on line 12

Notice: Undefined offset: 2 in C:\xampp\htdocs\address\to\file\founder_data_modal.php on line 13

Upvotes: 0

Views: 34

Answers (1)

failedCoder
failedCoder

Reputation: 1424

You get the error because there are no matches, your value doesn't match the pattern.

If the pattern is correct, check the $result variable before operating on it

if ($result) {
    var_dump($result);
    ... do something
} else {
    echo "No matches";
}

Or you can try to use different pattern that actually works for your data, for example if you want to check if the value contains only numbers and letters you can use this pattern:

A-Za-z0-9]+

Upvotes: 1

Related Questions