Jump to content

[SOLVED] preg_replace help


Brian W

Recommended Posts

Regular expressions irritate the living shit out of me. Though what I have doesn't give me some error, it just doesn't do anything, so I'm lost.

What I want it to do is replace " "(white space) with "_" for any text within quotes.

$Cleaned = preg_replace("/(\"[a-zA-Z0-9]\")/", str_replace(" ", "_", "$1"), $string);

but like I said, nothing happens to the string.

I've been testing with $string = "\"Word Word\""

 

any help is appreciated, thanks

Link to comment
https://forums.phpfreaks.com/topic/138923-solved-preg_replace-help/
Share on other sites

You may want to try some other tutorials via our resources.

 

These two paragraphs from your link cover character classes and quantifiers:

 

To specify a set of acceptable characters in your pattern, you can either build a character class yourself or use a predefined one. A character class lets you represent a bunch of characters as a single item in a regular expression. You can build your own character class by enclosing the acceptable characters in square brackets. A character class matches any one of the characters in the class. For example a character class [abc] matches a, b or c. To define a range of characters, just put the first and last characters in, separated by hyphen. For example, to match all alphanumeric characters: [a-zA-Z0-9]. You can also create a negated character class, which matches any character that is not in the class. To create a negated character class, begin the character class with ^: [^0-9].

 

The metacharacters +, *, ?, and {} affect the number of times a pattern should be matched. + means "Match one or more of the preceding expression", * means "Match zero or more of the preceding expression", and ? means "Match zero or one of the preceding expression". Curly braces {} can be used differently. With a single integer, {n} means "match exactly n occurrences of the preceding expression", with one integer and a comma, {n,} means "match n or more occurrences of the preceding expression", and with two comma-separated integers {n,m} means "match the previous character if it occurs at least n times, but no more than m times".

 

And here's a breakdown of the pattern:

NODE                    EXPLANATION

----------------------------------------------------------------------

  (                        group and capture to \1:

----------------------------------------------------------------------

    "                        '"'

----------------------------------------------------------------------

    [^"]+                    any character except: '"' (1 or more

                            times (matching the most amount

                            possible))

----------------------------------------------------------------------

    "                        '"'

----------------------------------------------------------------------

  )                        end of \1

----------------------------------------------------------------------

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.