Jump to content

[SOLVED] Can anyone lend a hand with str_replace please ?


_Chris

Recommended Posts

Hi all, Was recommended here as you're all a very helpful lot  :)  Basically, the code below, gets information from another page, but in the string for the day, it's coming in as the full name and for the date string, it's coming in as the full name for the month - with str_replace how do you get all days of the week and all months of the year, to change to the first 3 letters please ? ie, Monday changes to Mon, Tuesday to Tue, April changes to Apr, October changes to Oct?

 

$table8 .= "\n".'<td class="styles1"><div class="day">'.date('l',strtotime($dates[1][$i].' '.date('Y'))).'</div><div class="date">'.$datestr.'</div>'.str_replace('<span>Two</span>','',str_replace('Three.','Four',$Six[0][$i])).'</td>';

 

Any help much appreciated.

 

Chris.

 

 

Crikey, I was told that you lot were not only helpful but very quick as well - looks as though it was the total truth  :)  How would you change the code below to reflect the amendments please :

 

$table8 .= "\n".'<td class="styles1"><div class="day">'.date('l',strtotime($dates[1][$i].' '.date('Y'))).'</div><div class="date">'.$datestr.'</div>'.str_replace('<span>Two</span>','',str_replace('Three.','Four',$Six[0][$i])).'</td>';

 

Chris.

 

 

<?php

$table8 .= "\n".'<td class="styles1"><div class="day">'.substr(date('l',strtotime($dates[1][$i].' '.date('Y'))), 0, 3).'</div><div class="date">'.substr($datestr, 0, 3).'</div>'.str_replace('<span>Two</span>','',str_replace('Three.','Four',$Six[0][$i])).'</td>';

?>

 

^ Give that a whirl.

Change the 3 in the substr() methods to 6. It will cut off everything past the 6th character.

Unfortunately I'm not sure if you'd get values like "2 April" or if it would be "02 April" with the original string. Else it would give you "18 Apr" and "2 Apri" from using that method.

Many thanks for the quick help on this - it's really appreciated.  I'm now using the code below, and is giving the correct format, but are you saying that it would it give me 02 Apr on the 2nd of April, and not 2 Apr ?

 

<?php

 

$table8 .= "\n".'<td class="styles1"><div class="day">'.substr(date('l',strtotime($dates[1][$i].' '.date('Y'))), 0, 3).'</div><div class="date">'.substr($datestr, 0, 6).'</div>'.str_replace('<span>Two</span>','',str_replace('Three.','Four',$Six[0][$i])).'</td>';

 

?>

One thing someone was saying is that there is no need to use substr with l (lowercase L) because D can do exactly what you want. It's setup for MANY different types of output, all of which you can find online here.

 

So one thing you can do is change this:

substr(date('l',strtotime($dates[1][$i].' '.date('Y'))), 0, 3)

to:

date('D', strtotime($dates[1][$i].' '.date('Y')))

 

I'm not exactly sure what your variables hold, but I'd be willing to bet you could clean up a lot of your code in general by looking at what you can do with date/strtotime/mktime.

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.