Home > Enterprise >  how to search and cut certain word from particular database field using php codignater?
how to search and cut certain word from particular database field using php codignater?

Time:12-29

I want to give link to given number

notifications

reg no.12N34 has joined

one person left reg no F3659

Iam tried this code but didn't get expected output

$notification = $MYDATA['notification_content'];
$a = explode('no.',$notification);
print_r($a);

------expected output----------

12N34

F3659

CodePudding user response:

Assuming the registration numbers you're trying to match are all 5 characters long and only consist of capital letters and digits, you could use a regular expression, like this:

$notification = 'reg no.12N34 has joined one person left reg no F3659';
preg_match_all('![A-Z0-9]{5}!', $notification, $matches);
print_r($matches);

[A-Z0-9] matches only capital letters and digits

and {5} specifies this group should be exactly 5 characters in length

Edit: if there's only one registration number per row, preg_match might be better (preg_match stops searching after the first match, preg_match_all finds all of the matches in a string):

$notification = 'reg no.12N34 has joined';
preg_match('![A-Z0-9]{5}!', $notification, $matches);
print_r($matches);
  • Related