Reformat string containing date with Javascript

one solution without regex: var expDate=”2016-03″; var formatExp = expDate.split(‘-‘).reverse().join(“https://stackoverflow.com/”); //result is 03/2016 alert(‘result: ‘ + formatExp); var formatExpShort = expDate.substring(2).split(‘-‘).reverse().join(“https://stackoverflow.com/”); //result is 03/16 alert(‘result short: ‘ + formatExpShort);

Str_replace for multiple items

Like this: str_replace(array(‘:’, ‘\\’, “https://stackoverflow.com/”, ‘*’), ‘ ‘, $string); Or, in modern PHP (anything from 5.4 onwards), the slighty less wordy: str_replace([‘:’, ‘\\’, “https://stackoverflow.com/”, ‘*’], ‘ ‘, $string);

When to use strtr vs str_replace?

First difference: An interesting example of a different behaviour between strtr and str_replace is in the comments section of the PHP Manual: <?php $arrFrom = array(“1″,”2″,”3″,”B”); $arrTo = array(“A”,”B”,”C”,”D”); $word = “ZBB2″; echo str_replace($arrFrom, $arrTo, $word); ?> I would expect as result: “ZDDB” However, this return: “ZDDD” (Because B = D according to our array) … Read more

PHP explode the string, but treat words in quotes as a single word

You could use a preg_match_all(…): $text=”Lorem ipsum “dolor sit amet” consectetur “adipiscing \\”elit” dolor”; preg_match_all(“https://stackoverflow.com/”(?:\\\\.|[^\\\\”])*”|\S+/’, $text, $matches); print_r($matches); which will produce: Array ( [0] => Array ( [0] => Lorem [1] => ipsum [2] => “dolor sit amet” [3] => consectetur [4] => “adipiscing \”elit” [5] => dolor ) ) And as you can see, … Read more

PHP string replace match whole word

You want to use regular expressions. The \b matches a word boundary. $text = preg_replace(‘/\bHello\b/’, ‘NEW’, $text); If $text contains UTF-8 text, you’ll have to add the Unicode modifier “u”, so that non-latin characters are not misinterpreted as word boundaries: $text = preg_replace(‘/\bHello\b/u’, ‘NEW’, $text);