Plain form, without regex:
$prefix = 'bla_';
$str = 'bla_string_bla_bla_bla';
if (substr($str, 0, strlen($prefix)) == $prefix) {
$str = substr($str, strlen($prefix));
}
Takes: 0.0369 ms (0.000,036,954 seconds)
And with:
$prefix = 'bla_';
$str = 'bla_string_bla_bla_bla';
$str = preg_replace('/^' . preg_quote($prefix, '/') . '/', '', $str);
Takes: 0.1749 ms (0.000,174,999 seconds) the 1st run (compiling), and 0.0510 ms (0.000,051,021 seconds) after.
Profiled on my server, obviously.
Answer from Fabio Mora on Stack OverflowPlain form, without regex:
$prefix = 'bla_';
$str = 'bla_string_bla_bla_bla';
if (substr($str, 0, strlen($prefix)) == $prefix) {
$str = substr($str, strlen($prefix));
}
Takes: 0.0369 ms (0.000,036,954 seconds)
And with:
$prefix = 'bla_';
$str = 'bla_string_bla_bla_bla';
$str = preg_replace('/^' . preg_quote($prefix, '/') . '/', '', $str);
Takes: 0.1749 ms (0.000,174,999 seconds) the 1st run (compiling), and 0.0510 ms (0.000,051,021 seconds) after.
Profiled on my server, obviously.
You can use regular expressions with the caret symbol (^) which anchors the match to the beginning of the string:
$str = preg_replace('/^bla_/', '', $str);
You can use the DOM library:
$document = DOMDocument::loadHTML( $yourString );
$childToRemove = $document->getElementsByTagName('p')->item(0);
$childToRemove->parentNode->removeChild($childToRemove);
//get the string
return $document->saveHTML();
You can explode on line breaks and remove the first element with array_shift:
$html = '<p> </p>
<h2>Content</h2>
<p>Content</p>
<h2>Content</h2>
<p>Content</p>';
$lines = explode(PHP_EOL, $html); // break into lines
array_shift($lines); // remove first element
$newHtml = implode(PHP_EOL, $lines); // rejoin lines
echo $newHtml;
The substr() function will probably help you here:
$str = substr($str, 1);
Strings are indexed starting from 0, and this functions second parameter takes the cutstart. So make that 1, and the first char is gone.
To remove every : from the beginning of a string, you can use ltrim:
str = ltrim($str, ':');
var_dump($str); //=> 'f:o:'
The reason your ltrim code doesn't work is that you are passing in \1 which is not the same as the character 1. \1 refers to the character whose ASCII code is 1 which is not the same as 1 whose ASCII code is actually \49.
Modify your code like this:
ltrim($str, '1');
That should trim all 1s from the left of the string.
However, you should know that the ltrim will remove all matching characters from the left of the string, not just the first one!
If you want only the first, then you should use substr instead, with a test to make sure it is a 1.
if(substr($str, 0, 1) == '1')
$str = substr($str, 1);
And if you want to remove the period too, then simply modify the code to include that (and look at first 2 characters instead of only first character)
if (strlen($str) > 2 && substr($str, 0, 2) == '1.')
$str = substr($str, 2);
use strpos to check if 1. is at the beginning. If it is, then use substr to return the string minus the 1.
$string = '1.1';
if (strpos($string, '1.') === 0) {
$string = substr($string, 2);
}
var_dump($string);