6 Replies - 1706 Views - Last Post: 05 April 2012 - 06:37 AM Rate Topic: -----

#1 JonQCoder  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 23-May 11

preg_replace regular expression not working

Posted 04 April 2012 - 02:51 PM

I'm trying to remove a word, the word "the" to be precise from a title so the links will work correctly.

I thought it would just be this:

  $title = 'Brother! The house is on fire!';

  $sometitle = preg_replace('/^the$/','',$title);



or

$title = 'Brother! The house is on fire!';

  $sometitle = preg_replace("/^the$/","",$title);



But neither appear to do anything. I only want the word "the" removed when it is by itself. I tried str_replace as well, but it removed all instances of the word "the".
I'm not super experienced with regular expressions and most of what I found on google showed it like one of the two above. Any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: preg_replace regular expression not working

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 793
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: preg_replace regular expression not working

Posted 04 April 2012 - 02:59 PM

Have you tried using /\bthe\b/i as your match pattern? That should grab the word "the". There's a useful resource here for PHP regex.
Was This Post Helpful? 2
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • View blog
  • Posts: 5,972
  • Joined: 08-June 10

Re: preg_replace regular expression not working

Posted 04 April 2012 - 04:13 PM

The ^ and $ chars represent the beginning and end of a line. So if you do /^the$/, you are saying: "A line containing only the word 'the'."

What e_i_pi suggested uses the \b char instead, which represents the start or end of a word. That changes the expression to: "The distinct word 'the', anywhere in the line." - Also, the i after the expression makes it case-insensitive. They are case-sensitive by default.
Was This Post Helpful? 1
  • +
  • -

#4 sBorg  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 24-March 12

Re: preg_replace regular expression not working

Posted 05 April 2012 - 01:59 AM

Why do you even need to use preg_replace if you just intend replace the word "the" with ""?

A simple str_replace (or str_ireplace for case-insensitive search and replace) would do the job!
Was This Post Helpful? 0
  • +
  • -

#5 JonQCoder  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 23-May 11

Re: preg_replace regular expression not working

Posted 05 April 2012 - 06:11 AM

View Poste_i_pi, on 04 April 2012 - 02:59 PM, said:

Have you tried using /\bthe\b/i as your match pattern? That should grab the word "the". There's a useful resource here for PHP regex.

That worked! Did exactly what I wanted. I read the first little bit of that link before posting here, guess I misunderstood when he said ^ means any string that starts with something or $ means any string that ends with something. I assumed it meant any string within what you're searching through(Each individual word). XD

View PostAtli, on 04 April 2012 - 04:13 PM, said:

The ^ and $ chars represent the beginning and end of a line. So if you do /^the$/, you are saying: "A line containing only the word 'the'."

What e_i_pi suggested uses the \b char instead, which represents the start or end of a word. That changes the expression to: "The distinct word 'the', anywhere in the line." - Also, the i after the expression makes it case-insensitive. They are case-sensitive by default.

Thank you for explaining that. Makes more sense now. :)

View PostsBorg, on 05 April 2012 - 01:59 AM, said:

Why do you even need to use preg_replace if you just intend replace the word "the" with ""?

A simple str_replace (or str_ireplace for case-insensitive search and replace) would do the job!

I tried that, but it would replace 'the' everywhere in the string, even if it was in another word. So, "brother" would become bror, "other" would become or, etc.

This post has been edited by JonQCoder: 05 April 2012 - 06:13 AM

Was This Post Helpful? 0
  • +
  • -

#6 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 170
  • View blog
  • Posts: 608
  • Joined: 12-October 09

Re: preg_replace regular expression not working

Posted 05 April 2012 - 06:12 AM

Glad it's working now.

I find this tool very handy for working out preg_match's quickly:

http://www.solmetra.com/scripts/regex/
Was This Post Helpful? 1
  • +
  • -

#7 JonQCoder  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 23-May 11

Re: preg_replace regular expression not working

Posted 05 April 2012 - 06:37 AM

View PostDuckington, on 05 April 2012 - 06:12 AM, said:

Glad it's working now.

I find this tool very handy for working out preg_match's quickly:

http://www.solmetra.com/scripts/regex/

Wow, this will really be helpful. I've just been running the whole script every time I wanted to test it. Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1