Anchors
You may want to check for the presence of a pattern at the beginning or end of a string rather than simply checking to see if the pattern is contained anywhere within the string. The meta-characters for this are collectively referred to as anchors. <?php // Beginning of the string with a basic string function $start = (strpos($string, 'foo') === 0); // Same with a PCRE function $start = (preg_match('/~foo/', $string) == 1); // End of the string with basic string functions $end = (substr($string, - strlen('foo')) == 'foo'); // Same with a PCRE function $end = (preg_match('/foo$/', $string) == 1); // Means the same as an exact match with the string $equal = (preg_match('/~foo$/', $string) == 1); ?>
© PCRE Extension — Web Scraping >>> Back to TABLE OF CONTENTS <<< | |
Views: 372 | |
Total comments: 0 | |