Reputation: 465
i want to find the string after "-" which is exactly in 3rd position, If it doesn't find the "-" it should get the whole string. sample data are
TT-people // get people
1V-NEWTET // get NEWTET
ZZ-YESforTHE // get YESforTHE
Computer // get Computer
T-Book // get T-Book
I tried as
$result=preg_match_all("/^[a-zA-Z0-9]2-(\s\w*)/",$data,$networkID);
echo $networkID[0][1]
please rectify my error.
Upvotes: 1
Views: 41
Reputation: 3160
if its a single line if text you can do:
if(substr($string, 2, 1) === "-"){
echo substr($string, 3);
}else{
echo $string;
}
Upvotes: 1
Reputation: 224877
[a-zA-Z0-9]2
The 2
here just matches the character “2”. Looks like you meant {2}
.
To reflect your updated question, it should look like this:
$result = preg_match_all('/^(?:[a-zA-Z0-9]{2}-)?(\s\w*)/', $data, $networkID);
echo $networkID[0][1];
The ?
makes the group optional.
Upvotes: 1