Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Robert Walker
Robert Walker
17,146 Points

Replacing pattern from string.

I have a date which I formatted like so:

Fri, 15 May 2015 18:47:39 +0000

I want to get rid of the +0000 part from it, which after reading a few things and giving it a few attempts using preg_replace I keep getting errors like so.

Result:

<br /> <b>Warning</b>: preg_replace(): No ending delimiter '+' found in <b>[...][...]</b> on line <b>1</b><br /> 

PHP call:

<?php
$pattern = '+0000';
$replacement = '';
$string = 'Fri, 15 May 2015 18:47:39 +0000';
echo preg_replace($pattern, $replacement, $string);
?>

Any help?

3 Answers

jeremy rackley
jeremy rackley
22,336 Points

Okay I will answer this for you, but that is part of the struggle of learning to code, you have to be willing to fail over and over until you finally figure out the solution. Many programmers will not just answer the question for you because we want you to learn. The problem with your code is when you are declaring your pattern it needs to be in delimiters.(see http://php.net/manual/en/regexp.reference.delimiters.php)

So you need to declare the $pattern variable as $pattern = '/[0-9+]+$/'; What this is doing is saying get all numbers between 0-9 and the + at the end of the string. The $ means at the end.

I understand that you are frustrated as we all get trust me. Hope this helps you I have tested and it works

Robert Walker
Robert Walker
17,146 Points

Thank you that did indeed work, the frustrating part of it all is that I had that minus the + next to the $ symbol.

Every time I was doing it I would have /[0-9+]$/ and it would just remove all the numbers from the string, the addition of the + outside the range fixes that but if say for example it wasn't +0000 and instead +1234 that wouldn't work correct?

Also I know what you are saying about everyone needs to struggle but sometimes the best way is to give the answer and explain why it works, this being a prime example because I now understand it a lot better, some people like myself need to see how something works before they can understand it.

Thank you again for your reply and I hope I have not come across like an arse, most of the time I come on here and ask for help is in the small hours of the morning were I may not always word things the best way, again thank you for all your help.

jeremy rackley
jeremy rackley
22,336 Points

you can find any info you may need on php date formats by looking at the following link to the php manual http://php.net/manual/en/function.date.php

Robert Walker
Robert Walker
17,146 Points

Yes I understand the date formatting, if you read the question I am asking how to use preg_replace on a string and have provided the error I get with it too.

So thank you for the link to the documentation but it has zero relevance to me.

jeremy rackley
jeremy rackley
22,336 Points

try using [0-9+]+ for your regular expression, this should match all numbers between 0&9 as well as the + sign

jeremy rackley
jeremy rackley
22,336 Points

Just a hint for you to be able to figure out the rest

Robert Walker
Robert Walker
17,146 Points

No it really does not help at all, like I said I have read a few things about it and have also tried a number of different ways to solve the issue.

I just showed one error in particular trying to say subtly, I am a complete newbie to regular expressions and have no idea how im meant to do that but have tried a lot.

Also hints are great but when someone has already tried a load of different things and explains this in their post, hints are no good.

Thank you again for replying and trying to get me there on my own but that is not working for me at all, I cant match +0000 in the string, all other things I try give me errors or just remove all the numbers from the string.

Beating round the bush here really, I need someone to solve this for me and explain the workings of the solution please.