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 $

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

 

Before shooting yourself, read up on Regex and PHP Regex functions. str_replace() is not one of 'em.

 

$text = 'why won\'t you change my word. @@origionalWord@@';
echo preg_replace('/@@([a-z])+@@/i', 'replacementWord', $text);

 

Edit: meh

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.