Php replace everything before character

PHP string FAQ: How can I strip unwanted characters from a PHP string? (Also asked as, How can I delete unwanted characters from a PHP string?)

Solution

Here’s a quick PHP preg_replace example that takes a given input string, and strips all the characters from the string other than letters (the lowercase letters "a-z", and the uppercase letters "A-Z"):

$res = preg_replace("/[^a-zA-Z]/", "", $string);

If you put this line of code to work in a small PHP script, like this:

<?php

$string = "Th*()is 999 is <<>> a ~!@# sample st#$%ring.";
$res = preg_replace("/[^a-zA-Z]/", "", $string);
echo $res;

?>

and then run that script, you’ll get the following output:

Thisisasamplestring

As you can see, all the other characters have been stripped from the input string, leaving only letters in the resulting string.

Strip all characters but letters and numbers from a PHP string

Next, if we want to allow numbers as well as letters, we can modify our regular expression and preg_replace code to look like this:

$res = preg_replace("/[^a-zA-Z0-9]/", "", $string);

(I'll leave that output to you.)

Strip all characters but letters, numbers, and whitespace

Finally, if you want to strip all characters from your string other than letters, numbers, and whitespace, this regular expression will do the trick:

$res = preg_replace("/[^a-zA-Z0-9\s]/", "", $string);

(Again, I leave that output as "an exercise for the reader.")

Bonus: A complete PHP string-cleaning function

Before I go, I thought it might help to share the PHP function I wrote recently that led to this blog post.

For my purposes, I needed a PHP function to do the following things:

  • Strip all the characters from a string except the alphabet characters [a-zA-Z];
  • Limit the resulting string to eight characters;
  • Make the remaining characters lowercase.

Given those character-stripping needs, here's the source code for PHP function named cleanString I created:

<?php

// created by alvin alexander, alvinalexander.com

// take a given string,
// remove all chars except [a-zA-Z],
// make the string lowercase,
// limit the number of chars to 8.
function cleanString($string)
{
  // allow only letters
  $res = preg_replace("/[^a-zA-Z]/", "", $string);

  // trim what's left to 8 chars
  $res = substr($res, 0, 8);

  // make lowercase
  $res = strtolower($res);

  // return
  return $res;
}

// test the function
echo cleanString("iajaf1237412~!@#$%^&*()-=+_][{};:/.,<>?AAMNBVCXZLKJHG'\"");

?>

I'm still a relative PHP newbie, but I at least know that this code works, so I thought I'd share it here. Hopefully it can help someone else who needs to strip unwanted characters out of a PHP string.

  1. Home
  2. String
  3. Php Remove All Characters Before Specific String

Php replace everything before character
Php replace everything before character
Php replace everything before character
Php replace everything before character
Php replace everything before character
Php replace everything before character
Php replace everything before character
Php replace everything before character

PHP remove all characters before specific string

Tags: string , php Answers: 1 | Viewed 137,389 times

I need to remove all characters from any string before the occurrence of this inside the string:


"www/audio"

Not sure how I can do this.



xdazz answer at 2011-10-18 183


You can use strstr to do this.


echo strstr($str, 'www/audio');

* The answers/resolutions are collected from stackoverflow, are licensed under CC BY-SA 3.0

Some Code Answers


str_replace( $searchVal, $replaceVal, $subjectVal, $count )


Example to remove the Special Char

str_ireplace( $searchVal, $replaceVal, $subjectVal, $count )


Example to remove the Special Char

preg_replace( $pattern, $replacement, $subject, $limit, $count )


Example to remove the Special Char

Remove Special Character in PHP | Delft Stack

1 day ago $search_str: It contains such value which we want to search in the given string.$replace_str: It stores a value that you want to replace, or you can also leave it empty if you only want the removal of a special character.$main_str: It is the string we want to update.$count: It represents the number of characters we want to replace or remove.

Show details

See also: String Date

PHP remove all characters before certain string

6 days ago Web Apr 21, 2015  · Stack Overflow Public questions & answers; Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Talent Build …

Show details

See also: String

PHP remove all characters before specific string - PHPissue

1 day ago Web Sep 24, 2022  · [FIXED] PHP remove all characters before specific string . September 24, 2022 php, string No comments Issue. I need to remove all characters from any …

Show details

See also: Php String

PHP: Remove Everything Before and Including a Character/String

4 days ago Web This can be achieved using string manipulation functions in PHP. First we find the position of the desired character in the string using strpos(), substr(), ltrim, and trim()

Show details

See also: String Function

How to Remove Special Character from String in PHP

6 days ago Web May 31, 2020  · We have given a string and we need to remove special characters from string str in PHP, for this, we have the following methods in PHP: Using str_replace() …

Estimated Reading Time: 2 mins

Show details

See also: String

php remove string before specific character Code Example

1 week ago Web More “Kinda” Related PHP Answers View All PHP Answers » php replace space with underscore; php remove last character in string; php remove last char from string

Show details

See also: Php String

Use PHP to remove certain characters or words from a …

1 week ago Web Dec 05, 2019  · The following wrapper function shows how we can use PHP to remove specific characters from a value or text. We simply pass the text or value to the …

Show details

See also: Function

How to remove portion of a string after a certain …

1 week ago Web Feb 21, 2019  · The substr () and strpos () function is used to remove portion of string after certain character. strpos () function: This function is used to find the first occurrence …

Show details

See also: String Function

php remove string before specific character Code Example

6 days ago Web $fullpath = 'folderName/file.ext'; $folder = substr($fullpath, 0, strpos($fullpath, '/')); echo $folder; // Output => folderName

Show details

See also: Php String File

Remove Specific, Special Characters From String In PHP

2 days ago Web Jun 16, 2022  · Method 1: Remove Specific Character from String. We have one string if you want to remove specific characters from a string. So use the below function: The …

Show details

See also: String Function

How to remove special characters from a String in PHP

1 day ago Web Dec 18, 2021  · Now to remove the special characters from the string, we have to use a regular expression that matches any non-alphanumeric character as shown below. [^a …

Show details

See also: String Express

PHP remove all characters before specific string

5 days ago Web PHP remove all characters before specific string. होमपेज; php; php remove all characters before specific string "php remove all characters before specific string" …

Show details

See also: Php String

Please leave your answer here:

How do you remove portion of a string before a certain character in PHP?

The chop() function removes whitespaces or other predefined characters from the right end of a string.

How do I trim a string after a specific character in PHP?

The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.

What is str_ replace in PHP?

The str_replace() function replaces some characters with some other characters in a string. This function works by the following rules: If the string to be searched is an array, it returns an array. If the string to be searched is an array, find and replace is performed with every array element.

How do I remove all characters from a string after a specific character?

Using 'str. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.