Php string starts with numbers

str_starts_with

Performs a case-sensitive check indicating if haystack begins with needle .

Parameters

The substring to search for in the haystack .

Return Values

Returns true if haystack begins with needle , false otherwise.

Examples

Example #1 Using the empty string »

The above example will output:

All strings start with the empty string

Example #2 Showing case-sensitivity

$string = ‘The lazy fox jumped over the fence’ ;

if ( str_starts_with ( $string , ‘The’ )) echo «The string starts with ‘The’\n» ;
>

if ( str_starts_with ( $string , ‘the’ )) echo ‘The string starts with «the»‘ ;
> else echo ‘»the» was not found because the case does not match’ ;
>

The above example will output:

The string starts with 'The' "the" was not found because the case does not match

Notes

Note: This function is binary-safe.

See Also

  • str_contains() — Determine if a string contains a given substring
  • str_ends_with() — Checks if a string ends with a given substring
  • stripos() — Find the position of the first occurrence of a case-insensitive substring in a string
  • strrpos() — Find the position of the last occurrence of a substring in a string
  • strripos() — Find the position of the last occurrence of a case-insensitive substring in a string
  • strstr() — Find the first occurrence of a string
  • strpbrk() — Search a string for any of a set of characters
  • substr() — Return part of a string
  • preg_match() — Perform a regular expression match

User Contributed Notes 2 notes

With credit to Paul Phillips for the original polyfill posted.

If you do not have PHP 8, you can use these functions to get the capability of the new string functions.

But! Remember to use a conditional check to make sure the function is not already defined.

// source: Laravel Framework
// https://github.com/laravel/framework/blob/8.x/src/Illuminate/Support/Str.php
if (! function_exists ( ‘str_starts_with’ )) function str_starts_with ( $haystack , $needle ) return (string) $needle !== » && strncmp ( $haystack , $needle , strlen ( $needle )) === 0 ;
>
>
if (! function_exists ( ‘str_ends_with’ )) function str_ends_with ( $haystack , $needle ) return $needle !== » && substr ( $haystack , — strlen ( $needle )) === (string) $needle ;
>
>
if (! function_exists ( ‘str_contains’ )) function str_contains ( $haystack , $needle ) return $needle !== » && mb_strpos ( $haystack , $needle ) !== false ;
>
>
?>

This keeps it from breaking in case you upgrade and forget that you added it. This is a good practice generally when using the global scope for your helper functions.

In PHP7 you may want to use:

if (!function_exists(‘str_starts_with’)) function str_starts_with($str, $start) return (@substr_compare($str, $start, 0, strlen($start))==0);
>
>

AFAIK that is binary safe and doesn’t need additional checks.

Источник

Check if String Starts With a Number in PHP

This tutorial will discuss about unique ways to check if string starts with a number in php.

Table Of Contents

Method 1: Using Regex

PHP provides a function preg_match() , it accepts a regex pattern as first parameter and a string as the second parameter. If the string matches the given regex pattern, then it returns 1 otherwise it returns 0.

We can pass a regex pattern i.e. “/^2/” and given string into the preg_match() function, and if it returns 1, then it means that the string starts with a number. We have created a function for the same,

function startsWithNumber($str) < return preg_match('/^5/', $str) === 1; >

This function accepts a string as an argument and returns true if the string starts with the a number.

Frequently Asked:

Let’s see the complete example,

 $strValue = "221B Baker Street"; // Check if there is a Number at the beginning of String if (startsWithNumber($strValue)) < echo "The string starts with a number."; >else < echo "The string does not start with a number."; >?>
The string starts with a number.

Method 2: Using is_numeric() & ctype_digit() functions

The is_numeric() function accepts a variable as an argument and returns true, if the given variable is either in number or a number string. So we can use this to check if the first character of the string is a number or not. But with this, we need also need to use the ctype_digit() function. It accepts a string as an argument and returns true if all the characters in the provided string are numericals.

So we have created a function which accepts a string as an argument end returns true if the given string starts with a number i.e.

function startsWithNum($str)

Inside this function, we will fetch the first character of the string and then we will use the is_numeric() and ctype_digit() functions to check if the string starts with a numeric digit.

In the below example we will use this function to check if the string starts with a given number with any number in PHP

Let’s see the complete example,

 $strValue = "221B Baker Street"; // Check if there is a Number at the beginning of String if (startsWithNum($strValue)) < echo "The string starts with a number."; >else < echo "The string does not start with a number."; >?>
The string starts with a number.

Summary

Today, we learned about two ways to check if string starts with a Number in PHP.

Share your love

Leave a Comment Cancel Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Terms of Use

Disclaimer

Copyright © 2023 thisPointer

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.

The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.

The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.

The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.

Источник

PHP str_starts_with

Summary: in this tutorial, you’ll learn how to use the PHP str_starts_with() function to check if a string starts with a substring.

Introduction to the PHP str_starts_with() function

The str_starts_with() function performs a case-senstive search and checks if a string starts with a substring:

str_starts_with ( string $haystack , string $needle ) : boolCode language: PHP (php)

The str_starts_with() function has two parameters:

  • $haystack is the input string to check.
  • $needle is the substring to search for in the input string.

The str_starts_with() function returns true if the $haystack starts with the $needle or false otherwise.

The str_starts_with() function has been available since PHP 8.0.0. If you use a lower version of PHP, you can polyfill the function like this:

 if (!function_exists('str_starts_with')) < function str_starts_with($haystack, $needle) < return (string)$needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0; > >Code language: PHP (php)

PHP str_starts_with() function examples

Let’s take some examples of using the PHP str_starts_with() function.

1) Using PHP str_starts_with() function with single character example

The following example uses the PHP str_starts_with() function to check if the string ‘PHP tutorial’ starts with the letter ‘P’ :

 $str = 'PHP tutorial'; $substr = 'P'; $result = str_starts_with($str, $substr) ? 'is' : 'is not'; echo "The $str $result starting with $substr";Code language: PHP (php)
The PHP tutorial is starting with PCode language: PHP (php)

2) Using the PHP str_starts_with() function with multiple characters example

The following example uses the PHP str_starts_with() function to check if the string ‘PHP tutorial’ starts with the string ‘PHP’ :

 $str = 'PHP tutorial'; $substr = 'PHP'; $result = str_starts_with($str, $substr) ? 'is' : 'is not'; echo "The $str $result starting with $substr";Code language: PHP (php)
The PHP tutorial is starting with PHPCode language: PHP (php)

2) Case-sensitive example

It’s important to keep in mind that the str_starts_with() function performs a case-sensitive search. For example:

 $str = 'PHP tutorial'; $substr = 'php'; $result = str_starts_with($str, $substr) ? 'is' : 'is not'; echo "The $str $result starting with $substr";Code language: PHP (php)
The PHP tutorial is not starting with phpCode language: PHP (php)

Summary

  • Use the PHP str_starts_with() function to perform a case-sensitive search and check if a string starts with a substring.

Источник

Check if a String Starts With a Specified String in PHP

Check if a String Starts With a Specified String in PHP

  1. Use substr() Function to Check if a String Starts With a Specified String in PHP
  2. Use strpos() Function to Check if a String Starts With a Specified String in PHP
  3. Use strncmp() Function to Check if a String Starts With a Specified String in PHP

In this article, we will introduce methods to check if a string starts with a specified string in PHP.

Use substr() Function to Check if a String Starts With a Specified String in PHP

The built-in function substr() is used to access a substring. The string is passed as an input, and the substring that we want to access is returned. We can use this function to check if a string starts with a specific string. The correct syntax to use this function is as follows

substr($string, $startPosition, $lengthOfSubstring); 
php  $string = "Mr. Peter";  if(substr($string, 0, 3) === "Mr.")  echo "The string starts with the desired substring.";  >else  echo "The string does not start with the desired substring."; ?> 

In the above code, we want to check if our string starts with Mr. .

0 is the start index of the substring, or in other words, the substring starts from the first character of the given string.

3 means the length of the returned substring is 3.

If the start of the string is the same as Mr. then it will display The string starts with the desired substring. .

The string starts with the desired substring. 

Use strpos() Function to Check if a String Starts With a Specified String in PHP

The function strpos() returns the position of the first occurrence of a substring in the given string . We could use it to check if a string starts with a specified string.

If the returned value is 0 , it means the given string starts with the specified substring. Otherwise, the string doesn’t start with the checked substring.

strpos() is a case sensitive function. The correct syntax to use this function is as follows.

strpos($string, $searchString, $startPosition); 
php  $string = "Mr. Peter";  if(strpos( $string, "Mr." ) === 0)  echo "The string starts with the desired substring.";  >else  echo "The string does not start with the desired substring."; ?> 

Here, we have checked if our string starts with Mr. by finding the first occurrence of Mr. .

The string starts with the desired substring. 

Use strncmp() Function to Check if a String Starts With a Specified String in PHP

The built-in function strncmp() compares two given strings . This function is also case sensitive. The correct syntax to use this function is as follows.

strncmp($string1, $string2, $length); 

It returns zero if both the strings are equal. This is a case sensitive function.

php  $string = "Mr. Peter";  if(strncmp($string, "Mr.", 3) === 0)  echo "The string starts with the desired substring.";  >else  echo "The string does not start with the desired substring."; ?> 

Here, the two strings are compared. The length of the strings to be compared is three.

The string starts with the desired substring. 

The case insensitive version of strncmp is strncasecmp . It compares the first n characters of the given two strings regardless of their cases.

php  $string = "mr. Peter";  if(strncasecmp($string, "Mr.", 3) === 0)  echo "The string starts with the desired substring.";  >else  echo "The string does not start with the desired substring."; ?> 
The string starts with the desired substring. 

Related Article — PHP String

Источник

Читайте также:  Css button value text
Оцените статью