How to Remove All Specific Characters At the End of a String in PHP

How can I remove three characters at the end of a string in PHP?

Just do:

echo substr($string, 0, -3);

You don't need to use a strlen call, since, as noted in the substr documentation:

If length is given and is negative, then that many characters will be omitted from the end of string

How to Remove a specific character from the end of the string in php or regex

http://pl1.php.net/trim - that function gets characters to trim as last parameter

trim($string,"-");

or as suggested for right side only

rtrim($string,"-");

How do I remove all specific characters at the end of a string in PHP?

$output = rtrim($string, '.');

(Reference: rtrim on PHP.net)

Remove portion of a string after a certain character

$variable = substr($variable, 0, strpos($variable, "By"));

In plain english: Give me the part of the string starting at the beginning and ending at the position where you first encounter the deliminator.

Remove all special characters from a string

This should do what you're looking for:

function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.

return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}

Usage:

echo clean('a|"bc!@£de^&$f g');

Will output: abcdef-g

Edit:

Hey, just a quick question, how can I prevent multiple hyphens from being next to each other? and have them replaced with just 1?

function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.

return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
}

Php remove special chracter from from last charcters of string

You can use rtrim() if the special characters you want to remove are known before-hand:

$string = rtrim($string, ',".');

The second parameter describes the list of characters that should be removed.

Demo


If the special characters aren't known, then you could use a regex solution. The below preg_replace() statement will remove any character that's not a word character ([a-zA-Z0-9_], or a hyphen (-) or a single-quote ('):

preg_replace('/[^\w\-\']$/', '', $str);  

Explanation:

  • [^\w-\'] - match a single character not present in the list
    • \w - any word character [a-zA-Z0-9_]
    • \- - the literal character -
    • \' - the literal character '
  • $ - "end of line" anchor - used to assert position at end of the string

How to Remove a specific character from the end of the string in php

Since rtrim does nothing if the character at the end was not found, you can simply run it without the if check:

foreach($tag as $t) {
$t = rtrim($t);
$t = str_replace(' ', '-', $t);
$insert_value[] = '("'.$content_id.'","'.$t.'","'.time().'")';
}

Or even more reduced to:

foreach($tag as $t) {
$t = str_replace(' ', '-', rtrim($t));
$insert_value[] = '("'.$content_id.'","'.$t.'","'.time().'")';
}

However, this is just a hint to simplify your code. It should also work in it's current form as shown in the question, meaning the problem seems to be somewhere else.

How do I remove characters from the end of a string till I reach a certain character?

you need to use php's dirname function

$filename = 'test1/test.jpeg';
$filename2 = 'project/test2/test1112.jpeg';


var_dump(dirname($filename));
var_dump(dirname($filename2));

Remove portion of a string after a particular character from the end of the string

Use strrpos to get position of last matching element

//$variable = "8233 Station #2212"; //8233 Station
$variable = "8233 Station #2211 #2212";
echo trim(substr($variable, 0, strrpos($variable, "#"))); //8233 Station #2211


Related Topics



Leave a reply



Submit