Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple PHP Regex String Starts With

Tags:

regex

php

I am looking for the regex to stet if a string starts with another string. This has to be RegEx and it has to be PHP regex.

like image 543
Slappy Avatar asked Jun 16 '12 05:06

Slappy


1 Answers

$result = preg_match("#^startText(.*)$#i", $string);
if($result == 0)
{
    echo "No match";
}
else
{
    echo "Match found.";
}

PHP.net Regular Expressions

preg_match returns either 0 for no matches found, or 1, because preg_match stops at the first match. If you want to count all matches, use preg_match_all.

Check the PHP website if you have more trouble.

like image 96
Connor Deckers Avatar answered Oct 06 '22 01:10

Connor Deckers