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
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

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.