Jump to content

frickin damn regexp, please help me before I shoot myself.


dechamp

Recommended Posts

I'm not understanding what I'm doing wrong and I'm really tired which is my first problem. Solve this for me please!

 

$input = "why won't you change my word. @@origionalWord@@";

$q = "@@{1}[[:word:]]*@@{1}";

 

$input2 = str_replace($q, 'replacementWord', $input);

 

 

This is not changing it to "why won't you change my word. replacementWord

 

I've used an regexp gen to verify my work and it's still not working.

 

I tried a ton of different variations.

 

(@@{1}[[:word:]]*@@{1})

(@@){1}[[:word:]]*(@@){1}

((@@){1}[[:word:]]*(@@){1}) and so on. also with ^ and $

Link to comment
Share on other sites

Firstly str_replace doesn't support complex matching (i.e. Regular Expressions), it only matches literal strings. Secondly the syntax you are using there looks very much like POSIX, ideally you should be using PCRE syntax with the preg_ functions such as preg_match.

 

To replace literal '@@originalWord@@'

 

$output = str_replace('@@originalWord@@', 'replacementWord', $intput;

To replace any word between double @ signs

 

$output = preg_replace('#@@[a-z]+@@#i', 'replacementWord', $input);

 

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.