Jump to content

Recommended Posts

I need to replace \" for " and \' for ', but I'm having some problems. I tried a number of combinations but haven't found the right one. Example:

 

\"James O\'reilly\"

 

becomes

 

"James O'reilly"

 

Also, could someone link to the PHP manual page where I can learn about constructing a working delimiter string.

 

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

Also, could someone link to the PHP manual page where I can learn about constructing a working delimiter string.

 

Be more specific.  A string is a bunch of characters/numbers whatever.  A delimiter is something you use to mark separation in a string.  For example,

 

"some random text here"

 

the space could be considered a delimiter.  Or

 

"a,b,c,d,e"

 

the comma could be a delimiter. 

 

both of those examples are strings, as a whole.  The delimiters are used to enable doing certain things.  For example, explode each piece into an array:

 

$list = explode(",", "a,b,c,d,e");

 

Strangely stripslashes(), now works! It didn't before. It removed the paragraphs from the text.

 

What I mean about delimiters is in the following code named here:

 

preg_replace('here', 'here', $str);

 

I want to learn how to create them myself, for example, I don't know what /\s\s+/ means. Also I would like to know which characters have to be escaped and how etc...

 

Thanks for your help! You have helped me several times before, so thank you for that.  :)

 

the pcre regex functions (preg_xxx functions) requires the pattern to have a delimiter at the beginning and end of the pattern, so that it knows what the start and end of the pattern is.  The reason it requires it is because the pattern modifiers are also specified in the pattern string, outside of the delimiters. 

 

You can use pretty much anything that is not a letter or number like / ! # ~ < [ and you have to use the same character you on the end of the pattern as you do the beginning, like /..../ or !...! or ~....~  Exception to this is the < and [ in which case you would use its closing counterpart, like so: <...> [....] 

 

What delimiter you choose to use is mostly a matter of style.  Most people pick their poison and stick with it no matter what, but some people will alternate, depending on what kind of content they are working with.  For instance, if you are scraping pages with html in it, and you are using / as your delimiter, you will have to escape / every time you want to use it in your actual pattern, so that the regex engine doesn't get confused and think you are ending the pattern.  So, some people like to use something that won't be used in their regex pattern as their delimiter. 

 

Characters that must be escaped:

 

As mentioned, whenever you want to use your delimiter in your pattern, you need to escape it.  Also, you need to escape characters that have special meaning to the regex engine, if you want to use them literally.  Special characters include meta-characters and quantifiers like . * ? + For example, a * means match 0 or more of something.  If you are wanting the engine to specifically match a *, you would escape it \*  Other things you need to escape are [ ] and ( ) because the engine uses those as ways to group things.

 

If you see anything else escaped, like \s or \d or \1 or whatever, those are not really the same thing.  You don't need to escape an 's' or 'd' or '1' or whatever.  But some things take on special meaning when you do escape them.  Some things are turned into wildcards that match a range of certain similar things.  For instance, \s is shorthand for matching various 'spaces,' like a literal ' ' or tab or span.  \d is shorthand for matching any one digit or exponent.  Some escaped things act like variables for previously matched items (that you capture, not just match) in your pattern. 

 

 

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.