I need help with a regex, that I'm struggling with.
I'd like to convert:
9.15-11.15
to:
9:15-11:15
I have gotten as far as:
$message = preg_replace('/([0-9]{1,2}+)\.([0-9]{1,2}+)\-([0-9]{1,2}+)\.([0-9]{1,2}+)/', '$0:$1-$3:$4', $message);
This returns "9.15-11.15:9-11:15" though.
I'm not 100% sure if the issue is the match I'm making or my use of preg_replace?
echo str_replace('.', ':', '9.15-11.15' );
enjoy
If you do need a regex for that:
$message = 'Hello. This is some string with 9.15-11.15 time inside, and I don\'t want to replace the DOTS... inside';
$message = preg_replace('/(\d{1,2})\.(\d{1,2})\-(\d{1,2})\.(\d{1,2})/','$1:$2-$3:$4', $message);
var_dump($message);
// string(102) "Hello. This is some string with 9:15-11:15 time inside, and I don't want to replace the DOTS... inside"
Related
I know I can do a simple replace when wanting to convert <br> tags to new lines. But I am facing a problem with parsing because provided <br> tags are not empty.
<br style=\"color: rgb(83, 83, 83); font-family: \" helvetica=\"\" ...
Back end is not mine, so there is no point in discussing about good or bad coding here, I am just wondering if there is a solution to replace those with simple new lines.
Something like nl2br() but reverse.
EDIT:
Don't know what use is to show code when I know 'why' is the thing that I've tried not working...but here goes
public function removeSingleHtmlFormatting($single)
{
$single->short_description = str_replace("<br>", "\r\n", $single->short_description);
$single->short_description = strip_tags($single->description);
$single->short_description = preg_replace("/ /", " ", $single->short_description);
}
Of course replace doesn't work because there is no such string to replace...I have no idea where to start parsing it
instead of
str_replace("<br>", "\r\n", $single->short_description);
try
preg_replace("/<br.*>/U", "\r\n", $single->short_description);
This way the regular expression matches <br> including anything inside it, not only empty <br>.
Hello Stackers,
I'm just having a small PHP Question about the str_replace() Function. When I replace something, it will just replace everything; That's okay. But what I would like to know is this:
str_replace("*", "<strong>", $message);
Is it possible to use str_replace for codes like * This content is Bold *, just having the content, but still replacing the asterisk's with <strong> and </strong>?
Example:
Original: **This Should be Bold**
After Replacing: <strong>This Should be Bold</strong>
For people flagging this as a Duplicate: It's not about closing HTML Tags, it's about replacing.
I Hope I'm not that unclear. Thanks.
Use regular expression instead; it's more convenient:
$message = "**This Should be Bold**";
$message = preg_replace('#\**([^\*]+)\**#m', '<strong>$1</strong>', $message);
echo $message;
Or if you want to limit the number of asteroids to 2:
'#\*{1,2}([^\*]+)\*{1,2}#m'
You can also do like this
https://eval.in/518881
<?php
$string = '**This Should be Bold**';
$string = preg_replace("/\*\*(.+?)\*\*/", "<strong>$1</strong>", $string);
echo $string;
?>
function send_mails($adress, $subject, $message, &$tpl)
{
// fix for correct display of newlines and spaces in the message
$textmail = ereg_replace( "\n", "\r\n", $message);
$textmail = wordwrap($textmail, 70, "\r\n");
//prepare text to show online (html)
$textshow = ereg_replace( "\r\n", "<br>", $textmail);
$sent = '-- HTML Code to show the Mail you sent --';
$tpl->assign(BODY, $sent);
$mailtext = utf8_decode($textmail);
$headers = '-- Functional Header --';
mail ($adress, $subject, $mailtext, $headers);
}
My problem now is that if the message contains apostrophes they're show like this:
We\'re too good, so you shouldn\'t judge this example text\'s content
I've tried $textmail = ereg_replace(" \' ", " ' ", $textmail); but that didn't seem to work.
I'm pretty sure there is an easy fix, but I've been looking for some time now and haven't found a solution... Probably searching for the wrong thing.
string stripslashes ( string $str ) is what you are looking for!
$str = "We\'re too good, so you shouldn't judge this example text\'s content";
$newStr = stripslashes ( $str );
echo $newStr;
Output:
We're too good, so you shouldn't judge this example text's content
EDIT
VolkerK's comment:
I think it might be useful to first determine
whether this would fight the cause or just a symptom...
Please consider, that this is kind of Hot-Fix, you should try to find out, how and where this is actually happening?
Does $message at the beginning contain these slashes? (just echo it.)
If yes, where does $message come from?
If not, go through it all the way $message-> $textmail -> $textshow … and find out, which step does it and eliminate it there!
I'm trying to parse links using php with a structure such as [google](http://google.com), and so far I've got this:
"/\[(.+?)\]((.+?\))/is"
The only part I can't get to work is the part with the parenthesis ')'.
Any help would be great!
[edit]
#jari - This is part of the code:
$find = array(
"#\n#",
"/[**](.+?)[**]/is",
"/\[(.+?)\]\((.+?)\)/is"
);
$replace = array(
"<br />",
"<strong>$1</strong>",
"<a href\"$1\" target=\"_blank\" rel=\"no follow\"></a>"
);
$body = htmlspecialchars($body);
$body = preg_replace($find, $replace, $body);
return $body;
The parenthesis is a special character and usually marks sub-patterns inside your expression, so you need to escape them (just as you did with the square brackets, btw):
"/\[(.+?)\]\((.+?)\)/is"
It should look something like:
\[([^\]]+)]\([^)]+\)
We are using [^x] which means, match anything that is not x.
We have used this to capture in group one everything after the [ which is not a ].
Same for the second group using [^)]+. Anything that is not a ).
I have this replace regex (it's taken from the phpbb source code).
$match = array(
'#<!\-\- ([mw]) \-\-><a (?:class="[\w-]+" )?href="(.*?)" target\=\"_blank\">.*?</a><!\-\- \1 \-\->#',
'#<!\-\- .*? \-\->#s',
'#<.*?>#s',
);
$replace = array( '\2', '', '');
$message = preg_replace($match, $replace, $message);
If I run it through a message like this
asdfafdsfdfdsfds
<!-- m --><a class="postlink" href="http://website.com/link-is-looooooong.txt">http://website.com/link ... oooong.txt</a><!-- m -->
asdfafdsfdfdsfds4324
It returns this
asdfafdsfdfdsfds
http://website.com/link ... oooong.txt
asdfafdsfdfdsfds4324
However I would like to make it into a replace function. So I can replace the link title in a block by providing the href.
I want to provide the url, new url and new title. So I can run a regex with these variables.
$url = 'http://website.com/link-is-looooooong.txt';
$new_title = 'hello';
$new_url = 'http://otherwebsite.com/';
And it would return the same raw message but with the link changed.
<!-- m --><a class="postlink" href="http://otherwebsite.com/">hello</a><!-- m -->
I've tried tweaking it into something like this but I can't get it right. I don't know how to build up the matched result so it has the same format after replacing.
$message = preg_replace('#<!\-\- ([mw]) \-\-><a (?:class="[\w-]+" )?href="'.preg_quote($url).'" target\=\"_blank\">(.*?)</a><!\-\- \1 \-\->#', $replace, $message);
You'll find that parsing HTML with regex can be a pain and get very complex. Your best bet is to use a DOM parser, like this one, and modify the links with that instead.
You need to catch the other parts in groups as well and then use them in the replacement. try something like this:
$replace = '\1http://otherwebsite.com/\3hello\4';
$reg = '#(<!-- ([mw]) --><a (?:class="[\w-]+" )?href=")'.preg_quote($url).'("(?: target="_blank")?>).*?(</a><!-- \2 -->)#';
$message = preg_replace($reg, $replace, $message);
See here.