Simple php regex using preg_replace - php

I want to change strings with the odd """ such as:
He said: "I don't think so"
To be:
He said: "I don't think so"
My current code is:
$sentence = addslashes(preg_replace('/^\&quot\;$/','\"',$var));
What is my problem in the code?

^ and $ will only match the start and end of the whole string (or a whole line in /m mode). Since " doesn't appear like that, your regex whole match it. Just remove the ^ and $ and it should work.
BTW, perhaps you want to use html_entity_decode() instead.

You are probably better off using PHPs htmlspecialchars_decode():
$var = "He said: "I don't think so"";
$sentence = htmlspecialchars_decode($var);

This one may solve your problem:
$yourstring = "He said: "I don't think so"";
$newstring = str_replace(""","\"",$yourstring);
echo $newstring;

Related

Usin Preg_match_all with "\" character

I am trying to get a expression from a string using preg_match_all
but it doesn´t work correctly I try this:
$string='this is a test hello \1234';
preg_match_all('[(\\)(0-9)], $string, $output);
It only shows "hello" if I quit the "\" of the $string I can see "hello 1234" but with "\" It doesn´t works.
i think i might have a solution for you
$string='this is a test hello \1234';
preg_match("/hello \\\\[0-9]*/", $string, $output);
var_dump($output);
the thing here are the 4 backslashes. I'm not 100% sure about this but i think it is because in php you need to escape backslash so \\ produces \
and in the regular expression you need two backslashes as well (see praveen kumars answer for a good regexp explanation). So when you are using 4 backslashes, php changes that to 2 and that is what you need for this regexp
EDIT:
turns out there was some truth in my thoughts. check out this article for more explanation
If you use something like this:
/^(hello \\[0-9]*)$/g
This will give you the whole one. See the ( and [ variations.
Explanation

Php preg_split for forwardslash?

I've some text that i wish to parse
$str = "text1<br/>text2<br/>text3
I've tried using
print_r( preg_split("<br/>", $str));
but it is not giving me the desired output
Try the following:
$str = "text1<br/>text2<br/>text3";
print_r(preg_split("/<br\/>/", $str));
I'm assuming missing the closing quote " at the end of the $str = "text1<br/>text2<br/>text3" is just a typo.
Take a look at this page on how to specify the string $pattern parameter: http://php.net/manual/en/function.preg-split.php
It's because you're not using the correct regular expression. Is there a reason you can't use explode()? Regex is problematic, overly complicated at times, and much slower. If you know you'll always be splitting at the BR tag, explode is much more efficient.
Parsing HTML with regex is a bad idea, but here you go:
var_dump(preg_split('/(<br\ ?\/?>)+/', $str));

PHP Regex to remove everything after a character

So I've seen a couple articles that go a little too deep, so I'm not sure what to remove from the regex statements they make.
I've basically got this
foo:bar all the way to anotherfoo:bar;seg98y34g.?sdebvw h segvu (anything goes really)
I need a PHP regex to remove EVERYTHING after the colon. the first part can be any length (but it never contains a colon. so in both cases above I'd end up with
foo and anotherfoo
after doing something like this horrendous example of psuedo-code
$string = 'foo:bar';
$newstring = regex_to_remove_everything_after_":"($string);
EDIT
after posting this, would an explode() work reliably enough? Something like
$pieces = explode(':', 'foo:bar')
$newstring = $pieces[0];
explode would do what you're asking for, but you can make it one step by using current.
$beforeColon = current(explode(':', $string));
I would not use a regex here (that involves some work behind the scenes for a relatively simple action), nor would I use strpos with substr (as that would, effectively, be traversing the string twice). Most importantly, this provides the person who reads the code with an immediate, "Ah, yes, that is what the author is trying to do!" instead of, "Wait, what is happening again?"
The only exception to that is if you happen to know that the string is excessively long: I would not explode a 1 Gb file. Instead:
$beforeColon = substr($string, 0, strpos($string,':'));
I also feel substr isn't quite as easy to read: in current(explode you can see the delimiter immediately with no extra function calls and there is only one incident of the variable (which makes it less prone to human errors). Basically I read current(explode as "I am taking the first incident of anything prior to this string" as opposed to substr, which is "I am getting a substring starting at the 0 position and continuing until this string."
Your explode solution does the trick. If you really want to use regexes for some reason, you could simply do this:
$newstring = preg_replace("/(.*?):(.*)/", "$1", $string);
A bit more succinct than other examples:
current(explode(':', $string));
You can use RegEx that m.buettner wrote, but his example returns everything BEFORE ':', if you want everything after ':' just use $2 instead of $1:
$newstring = preg_replace("/(.*?):(.*)/", "$2", $string);
You could use something like the following. demo: http://codepad.org/bUXKN4el
<?php
$s = 'anotherfoo:bar;seg98y34g.?sdebvw h segvu';
$result = array_shift(explode(':', $s));
echo $result;
?>
Why do you want to use a regex?
list($beforeColon) = explode(':', $string);

Make me understand preg_replace

I've been looking all over the internet for some useful information and I think I found too much. I'm trying to understand regular expressions but don't get it.
Lets for instance say $data="A bunch of text [link=123] another bunch of text.", and it should get replaced with "< a href=\"123.html\">123< /a>".
I've been trying around a lot with code similar to this:
$find = "/[link=[([0-9])]/";
$replace = "< a href=\"$1\">$1< /a>";
echo preg_replace ($find, $replace, $data);
but the output is always the same as the original $data.
I think I have to see something relevent to my problem understand the basics.
Remove the extra [] around the (), and add + after the [0-9] to quantify it. Also, escape the [] that make up the tag itself.
$find = "/\[link=(\d+)\]/"; // "\d" is equivalent to "[0-9]"
$replace = "$1";
echo preg_replace($find,$replace,$data);
The regex would be \[link=([\d]+)\]
A good source for an quick overview of regular expression can you find here http://www.regular-expressions.info/
When you really interested in the power of regular expression, you should buy this book: Mastering Regular Expressions
A good Programm to test your RexEx on a Windows Client is: RegEx-Trainer
You are missing the + quantifier and as a result of this your pattern matches if there is a single digit following link=.
And there is an extra pair of [..] as a result of this the outer [...] will be treated as the character class.
You also forgot the escape the closing ].
Solution:
$find = "/[link=([0-9]+)\]/";
<?php
$data= "A bunch of text [link=123] another bunch of text.";
$find = '/\[link=([0-9]+?)\]/';
echo preg_replace($find, "$1", $data);

PHP Regex: Select all except last occurrence

I'm trying to replace all \n's sans that final one with \n\t in order to nicely indent for a recursive function.
This
that
then
thar
these
them
should become:
This
that
then
thar
these
them
This is what I have: preg_replace('/\n(.+?)\n/','\n\t$1\n',$var);
It currently spits this out:
This
that
then
thar
these
them
Quick Overview:
Need to indent every line less the first and last line using regex, how can I accomplish this?
You can use a lookahead:
$var = preg_replace('/\n(?=.*?\n)/', "\n\t", $var);
See it working here: ideone
After fixing a quotes issue, your output is actually like this:
This
that
then
thar
these
them
Use a positive lookahead to stop that trailing \n from getting eaten by the search regex. Your "cursor" was already set beyond it so only every other line was being rewritten; your match "zones" overlapped.
echo preg_replace('/\n(.+?)(?=\n)/', "\n\t$1", $input);
// newline-^ ^-text ^-lookahead ^- replacement
Live demo.
preg_replace('/\n(.+?)(?=\n)/',"\n\t$1",$var);
Modified the second \n to be the lookahead (?=\n), otherwise you'd run into issues with regex not recognizing overlapping matches.
http://ideone.com/1JHGY
Let the downwoting begin, but why use regex for this?
<?php
$e = explode("\n",$oldstr);
$str = $e[count($e) - 1];
unset($e[count($e) - 1]);
$str = implode("\n\t",$e)."\n".$str;
echo $str;
?>
Actually, str_replace has a "count" parameter, but I just can't seem to get it to work with php 5.3.0 (found a bug report). This should work:
<?php
$count = substr_count($oldstr,"\n") - 1;
$newstr = str_replace("\n","\n\t",$oldstr,&$count);
?>

Categories