Remove a word at the beginning of a string [duplicate] - php

This question already has answers here:
Remove a string from the beginning of a string
(10 answers)
Closed 9 years ago.
$str = "hello world, what's up";
How can I check $str to see if there is a word "hello" and remove it only if it is at the beginning of the string (first 5 letters)?

You can use substr, which is faster than preg_replace:
$str = "hello world, what's up?";
$pre = "hello ";
if(substr($str, 0, strlen($pre)) === $pre)
$str = substr($str, strlen($pre));
echo $str; // world, what's up?

^ indicates the beginning of the string, and an i flag for a case-insensitive match as per #Havenard's comment.
preg_replace('/^hello/i', '', $str);

preg_replace('/^hello\b/U', '', $str);
This will replace 'hello' in 'hello world' but not in 'helloworld'. Because it's replacing just one instance at the start of the string, the amount of cpu use is negligible.

Related

remove a part at the end of string - last match only [duplicate]

This question already has answers here:
Remove a part of a string, but only when it is at the end of the string
(8 answers)
Closed 12 months ago.
I'm using rtrim() to remove a part at the end of string, , US in my example:
<?php
$str = "Hello - world, c., US, US";
echo rtrim($str,", US");
?>
Output:
Hello - world, c.
It removed , US, US and i want to remove the last one only and the output should be Hello - world, c., US
How i can do that please?
rtrim() doesn't remove a specific string, it uses the string as a list of characters to remove at the end.
Use a regular expression replacement:
echo preg_replace('/, US$/', '', $str);
The $ anchors the match to the end of the string.
substr + strrpos approach:
$str = "Hello - world, c., US, US";
echo substr($str, 0, strrpos($str, ", US"));
The output:
Hello - world, c., US

replace first and last character in php [duplicate]

This question already has answers here:
Delete first 3 characters and last 3 characters from String PHP
(6 answers)
Closed 9 years ago.
A question about php.
If I have variable named $string that contains the word "Testing" I would like the php
code to delete the first and last character. (So the output would be "estin").
I've tried multiple functions for example str_replace and substr but so far I've only managed to delete only the first or only the last character.
I don't know how to delete both the first and last character.
<?php
$str = 'Testing';
$result = substr($str, 1, -1);
echo $result; // estin
the result of the code: http://codepad.org/RLbw4azA
read more about: substr
function: substr can receive 3 parameters:
string substr (string $string , int $start [, int $length ] )
If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes the position of this truncation or beyond, false will be returned.
use preg_replace
$string = preg_replace('/^.|.$/','',$string);
$str = "Testing";
echo $str = substr($str,1,-1);
Have you tried this:
$string = substr($string, 1, -1);
try substr($string, 1, -1). It will help
$str = 'ABCDEFGH';
echo $result = substr($str, 1, -1);
//output will be show the BCDEFG

Get rid of text in between () in PHP [duplicate]

This question already has answers here:
Remove Text Between Parentheses PHP
(7 answers)
Closed 9 years ago.
Say you have a string like this:
This is a string (with parenthesis stuff)
How would you change that to
This is a string
?
Replace it:
preg_replace('/\(.*?\)/', '', $str);
Use a regex.
Replace \([^)]*\) with the empty string.
Note: If there's more than one pair of parenthesis this will replace the innermost one.
try this
$string = "This is a string (with parenthesis stuff)";
echo preg_replace("/\([^)]+\)/","",$string); // 'ABC '
or you can do this also
$str = "This is a string (with parenthesis stuff)";
$str = trim(preg_replace('/\s*\([^)]*\)/', '', $str));

PHP string control on chars and numbers only [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Function to return only alpha-numeric characters from string?
Starting from $string = "hey hello 9times-%&";
I would like to replace all the chars that are NOT numeric[0-9] or [a-z,A-Z] type.
Is there a good method to show this process control?
EDITED
i forgot that i need to leave blank space bar blank spaces, i mean:
"hey &/k" must return as "hey k" and NOT as "heyk"
<?php
$string = "hey hello 9times-%&";
$string = preg_replace('/[^0-9A-Z\s]+/i', '', $string);
echo $string;
?>
preg_replace('/[^ \w]+/i', '', $string);
That will work as well. See the codepad example.
What about preg_replace:
$clean = preg_replace('/[^0-9a-z\s]/i','',$input);

Trim multiple characters using php [duplicate]

This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 12 months ago.
How to trim multiple characters at begin and end of string.
string should be something like {Hello {W}orld}.
i want to trim both { and } at begin and end.
don't want to use multiple trim function.
Use the optional second argument to trim which allows you to specify the list of characters to trim:
<?php
$str = "{Hello {W}orld}";
$str = trim($str, "{}");
echo "Trimmed: $str";
Output:
Trimmed: Hello {W}orld
Is there always a character at the beginning and at the end, that you want to remove? If so you could just use the following:
<?php
$string = '{Hello {W}orld}';
echo substr( $string, 1, strlen( $string )-2 );
?>
See: http://codepad.org/IDbG6Km2

Categories