Add substring at beginning of string if doesn't exist
Asked Answered
A

5

0

I want to add a substring (text) at beginning of string only if this string doesn't have already this text at beginning.

Example:

// let's say I want to add "Has" at beginning (if doesn't exist)

$string_1 = "AnaHasSomeApples"; // we need to add
$string_2 = "HsSomeApples"; // we need to add
$string_3 = "HasApplesAlready"; // already exists at the beginning

I tried this:

$string = (strpos($string, 'Has') === false ? 'Has' : '') . $string;

I know is not hard to do that.

I am more interested in finding the fastest (according to time, not lines of code) possible way.

Arguelles answered 20/6, 2019 at 16:21 Comment(6)
Whatever you do you're looking at an O(n) worst case complexity so just about anything you do is more or less going to be performing comparably.Macon
It looks like the thing you tried is just checking anywhere in the string, not just at the beginning.Riverside
You are right. My mistake.Arguelles
Just curious, is this something that's really slowing your code down at this point? There may be a better way to do it if you're doing this replacement enough times for the speed to have a significant impact.Riverside
String concatenation is what's going to be the heavy operation here, and it is also the operation you cannot avoid. What that does is byte by byte copy the first string to a new memory location, and then byte by byte copy the second string right after the first one. Compared to this the checking if the string starts with something is very fastMacon
@Don'tPanic, it happens in spl_autoload_register(). So I want to be sure that it takes very little.Arguelles
H
0

I am checking if Has is not at position 0 then prepend 'Has' with existing string
You can use ternary operator to achieve this,

$string_1 = "AnaHasSomeApples"; // we need to add
$string_2 = "HsSomeApples"; // we need to add
$string_3 = "HasApplesAlready"; // already exists at the beginning

echo "string_1: ". (strpos($string_1,"Has") !== 0 ? "Has".$string_1: $string_1)."\n";
echo "string_2: ". (strpos($string_2,"Has") !== 0 ? "Has".$string_2: $string_2)."\n";
echo "string_3: ". (strpos($string_3,"Has") !== 0 ? "Has".$string_3: $string_3)."\n";

Output

string_1: HasAnaHasSomeApples
string_2: HasHsSomeApples
string_3: HasApplesAlready

Demo.

Hargis answered 20/6, 2019 at 16:23 Comment(0)
B
2

You may like this solution:

First trim the string you want from main string to make sure that string doesn't exist at the begining of the main string, then try and add the string you want at the begining of the original string.

$string = 'HasSomeApples';

$result = 'Has' . ltrim($string, 'Has');
Banger answered 14/6, 2021 at 21:21 Comment(1)
This answer is not stable; ltrim() will greedily consume any leading characters found in the character mask. Proof: 3v4l.org/QL2VjTristis
G
1

You can try this way- to check the first 3 character is not equal "Has", if it has then just use the $string_1 else concatenate "Has" with it.

$string_1 = (substr( $string_1 , 0, 3 ) !== "Has") ? "Has".$string_1 : $string_1;
echo $string_1;

If you want "Has" to be case insensitive then you can use strtolower while using conditional check.

DEMO: https://3v4l.org/QEr1l

Giveaway answered 20/6, 2019 at 16:26 Comment(0)
H
0

I am checking if Has is not at position 0 then prepend 'Has' with existing string
You can use ternary operator to achieve this,

$string_1 = "AnaHasSomeApples"; // we need to add
$string_2 = "HsSomeApples"; // we need to add
$string_3 = "HasApplesAlready"; // already exists at the beginning

echo "string_1: ". (strpos($string_1,"Has") !== 0 ? "Has".$string_1: $string_1)."\n";
echo "string_2: ". (strpos($string_2,"Has") !== 0 ? "Has".$string_2: $string_2)."\n";
echo "string_3: ". (strpos($string_3,"Has") !== 0 ? "Has".$string_3: $string_3)."\n";

Output

string_1: HasAnaHasSomeApples
string_2: HasHsSomeApples
string_3: HasApplesAlready

Demo.

Hargis answered 20/6, 2019 at 16:23 Comment(0)
T
0

It's not likely to be the fastest (although preg_replace() does work directly on an array if that's what you have), but if you'd like to avoid writing a condition, you can use a regex pattern. Unless you are doing 100,000's of iterations, speed is not likely to be a real concern for this task.

If Has isn't at the front of the string, add it to the front of the string.

Code: (Demo)

echo preg_replace('/^(?!Has)/', 'Has', $string);
// HasAnaHasSomeApples
// HasHsSomeApples
// HasApplesAlready
Tristis answered 9/9, 2023 at 12:38 Comment(0)
T
0

Modern day solution, like PHP 8 kind of solution, using the str_starts_with function

$string_1 = "AnaHasSomeApples"; // we need to add
$string_2 = "HsSomeApples"; // we need to add
$string_3 = "HasApplesAlready"; // already exists at the beginning
echo str_starts_with($string_1, 'Has') ? $string_1 : "Has$string_1<br>";
echo str_starts_with($string_2, 'Has') ? $string_2 : "Has$string_2<br>";
echo str_starts_with($string_3, 'Has') ? $string_3 : "Has$string_3<br>";

Output:

HasAnaHasSomeApples
HasHsSomeApples
HasApplesAlready
Tokyo answered 6/10, 2024 at 21:23 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.