Startswith() and Endswith() Functions in PHP

startsWith() and endsWith() functions in PHP

PHP 8.0 and higher

Since PHP 8.0 you can use the

str_starts_with
Manual
and

str_ends_with Manual

Example

echo str_starts_with($str, '|');

PHP before 8.0

function startsWith( $haystack, $needle ) {
$length = strlen( $needle );
return substr( $haystack, 0, $length ) === $needle;
}
function endsWith( $haystack, $needle ) {
$length = strlen( $needle );
if( !$length ) {
return true;
}
return substr( $haystack, -$length ) === $needle;
}

startsWith() and endsWith() functions in PHP

PHP 8.0 and higher

Since PHP 8.0 you can use the

str_starts_with
Manual
and

str_ends_with Manual

Example

echo str_starts_with($str, '|');

PHP before 8.0

function startsWith( $haystack, $needle ) {
$length = strlen( $needle );
return substr( $haystack, 0, $length ) === $needle;
}
function endsWith( $haystack, $needle ) {
$length = strlen( $needle );
if( !$length ) {
return true;
}
return substr( $haystack, -$length ) === $needle;
}

Does R have function startswith or endswith like python?

Not inbuilt like that.

Options include grepl and substr.

x <- 'ABCDE'
grepl('^AB', x) # starts with AB?
grepl('DE$', x) # ends with DE?
substr(x, 1, 2) == 'AB'
substr('ABCDE', nchar(x)-1, nchar(x)) == 'DE'

How to check if a string starts with a specified string?

PHP 8 or newer:

Use the str_starts_with function:

str_starts_with('http://www.google.com', 'http')

PHP 7 or older:

Use the substr function to return a part of a string.

substr( $string_n, 0, 4 ) === "http"

If you're trying to make sure it's not another protocol. I'd use http:// instead, since https would also match, and other things such as http-protocol.com.

substr( $string_n, 0, 7 ) === "http://"

And in general:

substr($string, 0, strlen($query)) === $query

No startswith,endswith functions in Go?

The strings package contains HasPrefix and HasSuffix.

import "strings"

startsWith := strings.HasPrefix("prefix", "pre") // true
endsWith := strings.HasSuffix("suffix", "fix") // true

play.golang.org

Join string based on startsWith() and endsWith()

i think it would be a good idea to add the function to the String's prototype and using startsWith() and Conditional (ternary) Operator this what i could come up with :

String.prototype.merge = function(str) {  let match;  for (let i = this.length; i >= 0; i--)    (str.startsWith(this.slice(i))) && (match = this.slice(i));
return this.slice(0, this.indexOf(match)) + str.slice(str.indexOf(match), str.length)}
let merged = 'foobarbaz'.merge('bazfoo')
console.log(merged);

how to know if a $string ends with ','?

There are a few options:

if (substr($string, -1) == ',') {

Or (slightly less readable):

if ($string[strlen($string) - 1] == ',') {

Or (even less readable):

if (strrpos($string, ',') == strlen($string) - 1) {

Or (even worse yet):

if (preg_match('/,$/', $string)) {

Or (wow this is bad):

if (end(explode(',', $string)) == '') {

The take away, is just use substr($string, -1) and be done with it. But there are many other alternatives out there...



Related Topics



Leave a reply



Submit