Need to insert - into a variable holding a date? - php

I have a date variable with the format
2008 12 29
to have it correctly display from within my database app I need the format to be
2008-12-29
Is there a way to simply add the - into the string or replace the spaces with -?
I am using PHP and the date is stored in $release_date

Use str_replace():
$release_date = str_replace(' ', '-', $release_date);

The str_replace() method is what you search for:
$good_format_date = str_replace(' ', '-', $date);

If you know for a fact that the spaces will always be standard (spacebar) spaces, use str_replace() as BoltClock said.
However, if it's possible that there may be extra spaces, tabs, or other whitespace characters in between your date parts, use preg_replace() as it will work in almost all cases unlike str_replace():
$release_date = preg_replace( '/\s+/', '-', $release_date );

Related

Need to replace repeating string with single instance

Ok so I am taking a string, querying a database and then must provide a URL back to the page. There are multiple special characters in the input and I am stripping all special characters and spaces out using the following code and replacing with HTML "%25" so that my legacy system correctly searches for the value needed. What I need to do however is cut down the number of "%25" that show up.
My current code would replace something like
"Hello. / there Wilbur" with "Hello%25%25%25%25there%25Wilbur"
but I would like it to return
"Hello%25there%25Wilbur"
replacing multiples of the "%25" with only one instance
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
return preg_replace('/[^A-Za-z0-9]/', '%25', $string); // Replaces special chars.
Just add a + after selecting a non-alphanumeric character.
$string = "Hello. / there Wilbur";
$string = str_replace(' ', '-', $string);
// Just add a '+'. It will remove one or more consecutive instances of illegal
// characters with '%25'
return preg_replace('/[^A-Za-z0-9]+/', '%25', $string);
Sample input: Hello. / there Wilbur
Sample output: Hello%25there%25Wilbur
This will work:
while (strpos('%25%25', $str) !== false)
$str = str_replace('%25%25', '%25', $str);
Or using a regexp:
preg_replace('#((?:\%25){2,})#', '%25', $string_to_replace_in)
No looping using a while, so the more consecutive '%25', the faster preg_replace is against a while.
Cf PHP doc:
http://fr2.php.net/manual/en/function.preg-replace.php

PHP str_replace replace space with dash, but too many dash

I have this string here photo of the day - 2011 and I need to look like photo-of-the-day-2011 when I do a str_replace, I get this output:
photo-of-the-day---2011 what can I do to fix this?
Here is the code:
$albumName = 'photo of the day - 2011'; (this comes from a db and cannot change it)
$albumName = str_replace(" ", "-", $albumName);
You can use the more powerful preg_replace, instructing it to replace runs of dashes and/or spaces with a single dash:
$name = preg_replace('/[\s-]+/', '-', $name);
[\s-]+ is a regular expression that matches "one or more of: whitespace and dashes".

I have some trouble with replacing a word AND a special character with whitespace, using preg_replace

This is my code at the moment:
$title = preg_replace('/\bReactionDB\b/i',' ', $title_matches[1]);
The input text = "It Begins! | ReactionDB"
In addition to "ReactionDB", I want it to replace the character "|" with nothing/whitespace.
I've searched for hours and I'm finding no solution, anyone know what to do?
Make a second call to preg_replace:
$title = preg_replace('/\bReactionDB\b/i',' ', $title_matches[1]);
$title = preg_replace('/\|/', '', $title);
Note that you need to escape | as it is a special character.

How do I strip all spaces out of a string in PHP? [duplicate]

This question already has answers here:
How can strip whitespaces in PHP's variable?
(15 answers)
Closed 3 years ago.
How can I strip / remove all spaces of a string in PHP?
I have a string like $string = "this is my string";
The output should be "thisismystring"
How can I do that?
Do you just mean spaces or all whitespace?
For just spaces, use str_replace:
$string = str_replace(' ', '', $string);
For all whitespace (including tabs and line ends), use preg_replace:
$string = preg_replace('/\s+/', '', $string);
(From here).
If you want to remove all whitespace:
$str = preg_replace('/\s+/', '', $str);
See the 5th example on the preg_replace documentation. (Note I originally copied that here.)
Edit: commenters pointed out, and are correct, that str_replace is better than preg_replace if you really just want to remove the space character. The reason to use preg_replace would be to remove all whitespace (including tabs, etc.).
If you know the white space is only due to spaces, you can use:
$string = str_replace(' ','',$string);
But if it could be due to space, tab...you can use:
$string = preg_replace('/\s+/','',$string);
str_replace will do the trick thusly
$new_str = str_replace(' ', '', $old_str);

How to do a regex replacement, adding characters in a date string?

For PHP
I have a date I want line wrapped.
I have $date = '2008-09-28 9:19 pm';
I need the first space replaced with a br
to become
2008-09-28<br>9:19 pm
If it wasn't for that second space before PM, I would just str_replace() it.
$date = preg_replace('/ /', '<br>', $date, 1);
s/ /<br\/>/ should do it. Though PHP regex might be greedy and replace all spaces.
edit
I support Ben Hoffstein's PHP solution. Where possible, avoid regex as it nearly always has unintended side effects.
$date = '2008-09-28 9:19 pm';
$result = preg_replace('/(\d{4}-\d{2}-\d{2}) (.*)/', '$1<br>$2', $date);
If you want a regex to match the pattern and return both parts, you can use the following. However, considering all that you're doing is replacing only 1 space, try the str_replace_once that I'll suggest after the regex. Regex is for complicated parsing instead of a wasteful use like replacing one space (no offense intended).
Please note that the following is browser-code, so don't attempt to use it verbatim. Just in case of a typo.
$regex = '/([\d]{4}-[\d]{2}-[\d]{2}) ([\d]{1,2}:[\d]{2} (am|pm))/';
$match = preg_match($regex, '2008-09-28 9:19 pm');
print $match[1]; // Returns 2008-09-28
print $match[2]; // Returns 9:19 pm
// Or replace:
preg_replace($regex, '$1<br>$2', $date);
I suggest the following, much faster mechanism. See this post for the function str_replace_once().
str_replace_once(' ', '<br>', $date);
The other way to do this, which is faster but a bit more code, is
$date = substr($date, 0, 10) . '<br>' . substr($date, 11);

Categories