replace first and last character in php [duplicate] - php

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

Related

Remove last characters of strings using regex [duplicate]

This question already has answers here:
PHP substring extraction. Get the string before the first '/' or the whole string
(14 answers)
Closed 12 months ago.
I need to find a way in PHP to remove the last portions of 2 strings using regex's. This way once they are stripped of the extra characters I can find a match between them. Here is an example of the type of string data I am dealing with:
categories_widget-__i__
categories_widget-10
So I would like to remove:
-__i__ from the first string
-10 from the second string
Thanks in advance.
(.*)-
This simple regex can do your job if - is the splitting criteria
See demo.
http://regex101.com/r/rX0dM7/7
$str1 = "categories_widget-__i__";
$str2 = "categories_widget-10";
$arr1 = explode("-", $str1);
$arr2 = explode("-", $str2);
echo $arr1[0];
echo $arr2[0];
Is the last occurrence of a hyphen the only thing that's important? If so you don't need regex:
$firstPart = substr($str, 0, strrpos($str, '-'));
ยป example
You could try the below code to remove all the characters from - upto the last.
<?php
$text = <<<EOD
categories_widget-__i__
categories_widget-10
EOD;
echo preg_replace("~-.*$~m","",$text);
?>
Output:
categories_widget
categories_widget
- matches the literal - symbol. And .* matches any character following the - symbol upto the end of the line. $ denotes the end of a line. By replacing all the matched characters with an empty string would give you the desired output.

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

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.

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

Delete first 3 characters and last 3 characters from String PHP

I need to delete the first 3 letters of a string and the last 3 letters of a string. I know I can use substr() to start at a certain character but if I need to strip both first and last characters i'm not sure if I can actually use this. Any suggestions?
Pass a negative value as the length argument (the 3rd argument) to substr(), like:
$result = substr($string, 3, -3);
So this:
<?php
$string = "Sean Bright";
$string = substr($string, 3, -3);
echo $string;
?>
Outputs:
n Bri
Use
substr($var,1,-1)
this will always get first and last without having to use strlen.
Example:
<?php
$input = ",a,b,d,e,f,";
$output = substr($input, 1, -1);
echo $output;
?>
Output:
a,b,d,e,f
As stated in other answers you can use one of the following functions to reach your goal:
substr($string, 3,
-3) removes 3 chars from start and end
trim($string, ",") removes all specific chars from start and end
ltrim($string, ".") removes all specific chars from start
rtrim($string, ";") removes all specific chars from end
It depends on the amount of chars you need to remove and if the removal needs to be specific. But finally substr() answers your question perfectly.
Maybe someone thinks about removing the first/last char through string dereferencing. Forget that, it will not work as null is a char as well:
<?php
$string = 'Stackoverflow';
var_dump($string);
$string[0] = null;
var_dump($string);
$string[0] = null;
var_dump($string);
echo ord($string[0]) . PHP_EOL;
$string[1] = '';
var_dump($string);
echo ord($string[1]) . PHP_EOL;
?>
returns:
string(13) "Stackoverflow"
string(13) "tackoverflow"
string(13) "tackoverflow"
0
string(13) "ackoverflow"
0
And it is not possible to use unset($string[0]) for strings:
Fatal error: Cannot unset string offsets in /usr/www/***.php on line **
substr($string, 3, strlen($string) - 6)
I don't know php, but can't you take the length of the string, start as position 3 and take length-6 characters using substr?
$myString='123456789';
$newString=substr($myString,3,-3);

Get part of string using php

How to get a part of string using PHP?
I have a string like this.
$str = 'href="http://www.idontknow.com/areyousure?answer=yes"';
I want only the link.. like this
$str_new = "http://www.idontknow.com/areyousure?answer=yes";
$str_new = substr($str, 6, -1);
substr()
If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string).
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.
If length is given and is 0, FALSE or NULL an empty string will be returned.
If length is omitted, the substring starting from start until the end of the string will be returned.
$str = 'href="http://www.idontknow.com/areyousure?answer=yes"';
preg_match('/href="(.*)"/', $str, $matches);
$str_new = $matches[1];
echo $str_new;
Output:
http://www.idontknow.com/areyousure?answer=yes
Try
$result = substr($input, 6, strlen($input) - 1);
Use a regular expression:
$str = 'href="http://www.idontknow.com/areyousure?answer=yes"';
$string = preg_replace ( '/href="(.*)"/', '\1', $str );
$str = preg_replace('/href=/i', '', $str);

Categories