Reputation: 1422
I am trying to preg_match a string .basically if i got a string which is given to a variable
$line = " <szFilename>  ds </szFilename> "
And I need to check if the sting starts with "<szFilename>" .
am finding a hard time finding the pattern.
What i tried is
$line=" <szFilename>  ds </szFilename> ";
//$line2=" <szFilenamed1>  ds </szFilenamed1> ";
//echo trim($line);
if(preg_match("/[<szFilename>]*/",trim($line))){
echo "GOT IT";
}
Any help?
Upvotes: 0
Views: 46
Reputation: 59681
No need for a regex here. Just simply use strpos()
to determinate if the string is at the start (index 0) or not, e.g.
if(strpos(trim($line), "<szFilename>") === 0) {
echo "success";
}
If you want/need to use a regex, just use an anchor (^
) to match your regex at the start, e.g.
if(preg_match("/^<szFilename>/", trim($line))) {
echo "success";
}
Also if you are writing your own regex's, I can highly recommend this site: https://regex101.com/, which highlights everything nicely and you get an nice explanation to the right, what your regex does.
Upvotes: 4