I just want to replace "doi:yyyy.yyyy" here, but $1 does not give me back that text. Why? ;-)
$zp_citation['citation'] = preg_replace('(doi:[^ <]*)',
'<b>' . '$1' . " - where did doi go?" . '</b>',
$zp_citation['citation'],
1);
You are missing required regex delimiters:
$zp_citation['citation'] = preg_replace('/(doi:[^ <]*)/',
'<b>' . '$1' . " - where did doi go?" . '</b>',
$zp_citation['citation'], 1);
Related
I haven't used regex in a long time and can't figure out how to match an actual dollar symbol and any reference to dollar symbol and regex tells me about the special meanings and cases. I need to match a $. I expected that \$ or $$ was supposed to escape it, but I'm still not matching it.
Here's my text
(WW) Capacity Charge
. . . . . . . . . . . . . . . . $ 123.45
WW Commodity Charge . . . . . . . . . $ 67.89
I'm trying to capture 123.45 I figured I should just match the first occurrence where some characters are sandwiched between the dollar symbol, space and a newline. Here are a few of the regexes I've tried.
preg_match("|(?<=\$\s)(.*)(?=\n)|",$data[1],$matches); //no matches
preg_match("|(?<=$\s)(.*)(?=\n)|",$data[1],$matches); //no matches
preg_match("|(?<=$)(.*)(?=\n)|",$data[1],$matches); //no matches
preg_match("|(?<=\$)(.*)(?=\n)|",$data[1],$matches); //no matches
preg_match("|(?<=$$)(.*)(?=\n)|",$data[1],$matches); //no matches
Just to check that something matches I even did
preg_match("|(?<=\.)(.*)(?=\n)|",$data[1],$matches); // . . . . . . . . . . . . . . . $ 123.45
preg_match("|(?<=.)(.*)(?=\n)|",$data[1],$matches); // . . . . . . . . . . . . . . . $ 123.45
preg_match("|(?<=1)(.*)(?=\n)|",$data[1],$matches); // 23.45
How can I match the text between the $ and the newline?
You are in double quotes so you need to escape twice (once for PHP, then once for PCRE). I prefer a character class though because that works in all regex flavors.
(?<=[$]\s)(.*)(?=\n)
I have a problem with preg_replace in PHP.
My text:
[Derp] a
• [Derp] a
My regex:
$simple_search[0] = '/\[(.*?)\] (.*?)/is';
$simple_search[1] = '/\• \[(.*?)\] (.*?)/is';
My subject:
$simple_replace[0] = "[color=#009D9D][$1][/color] $2";
$simple_replace[1] = "[color=#30BA76]• [$1][/color] [color=#92CF91]$2[/color]";
After preg_replace:
[color=#009D9D][Derp][/color] a
[color=#30BA76]#color=#009D9D][Derp][/color[/color] [color=#606090]: [/color]a
(it's a tool for coloring quotes)
[Derp] a and
• [Derp] a must not have the same color.
The problem is that the first search then replaces that this is not the right thing.
How can I detect that research is equal to the string?
replace your first regexp:
/(?<!\• )\[(.*?)\] (.*?)/is
means can not have front of the "[" an "•" and a space. Also if the • stands in beginning of your lines then you could put ^ front of it
$str = '[Derp] a
• [Derp] a';
$simple_search[0] = '/(\• )?(?P<m2>\[.*?\]) (?P<m3>.*)/i';
echo $str = preg_replace_callback($simple_search[0],
function ($m) {
if (!$m[1]) return '[color=#009D9D]' . $m[2] . '[/color] ' . $m[3];
else return '[color=#30BA76]• ' . $m[2] . '[/color] [color=#92CF91]' . $m[3] . '[/color]';
}, $str
);
result
[color=#009D9D][Derp][/color] a
[color=#30BA76]• [Derp][/color] [color=#92CF91]a[/color]
I have a value saved in array $phone and want to remove "Tel. " and " - ". The orignal value of $phone is "Tel. +47 - 87654321" and I want it to be "+4787654321".
So far I have made this code:
$phone = Tel. +47 - 87654321;
echo "<br />"
. str_replace("Tel. ","",($phone->textContent)
. str_replace(" - ","",$phone->textContent));
This results:
+47 - 87654321+4787654321
How can I avoid printing (echo) the first part of this code? Is it a better way to do this?
That worked for me:
echo "<br />" . str_replace(array("Tel. ", " - "), "", $phone->textContent);
Don't concatenate results from both functions because they're returning whole string after changes.
Instead change it in next calls
$string = str_replace("Tel. ", "", $phone->textContent);
$string = str_replace(" - ", "", $string);
or pass array of items to str_replace().
$string = str_replace(array("Tel. ", " - "), "", $phone->textContent);
Assign your intermediate result to a variable. Try:
$temp = str_replace("Tel. ","",($phone->textContent);
echo "<br />". str_replace(" - ","",$temp));
echo preg_replace ("/^(.*)(?=\\+)(.*)$/", "$2", "Tel. +47 - 87654321" )
will output:
+47 - 87654321
Explanation:
it uses regex in order to divide the string into two groups:
all the characters that come before the + (using lookahead)
anything that comes after the + including the +
and then replace it only with the second group
can someone help me on how to remove spaces in my sql.
for exmaple
let say i type in "I am a good boy"... i want it to save in my mysql table column as "iamagoodboy" removing all spaces of anything i send.. where in this code below can i do this, thanks very much
$sql = 'INSERT INTO messages (username,usernameto, message_content, message_time) VALUES ("' . $username . '", "' . $messageTo . '", "' . $message . '", ' . $time . ')';
$result = mysql_query($sql, $cn) or
die(mysql_error($cn));
str_replace(' ', '', $message);
Should work fine for you in PHP. As a general rule, don't put that sort of functionality on the Database, no reason to put the load on that server - do it on the web server instead.
So your code would look like this (assuming you are taking the spaces out of $message):
$sql = 'INSERT INTO messages (username,usernameto, message_content, message_time) VALUES ("' . $username . '", "' . $messageTo . '", "' . str_replace(' ', '', $message) . '", ' . $time . ')';
A better solution, though, might be to use preg_replace('/\s+/', '', $string); which will strip all whitespace (tabs, linebreaks, etc). Depends on what exactly you want to accomplish.
Use str_replace:
$string = str_replace(' ', '', $string);
or remove all whitespace
$string = preg_replace('/\s+/', '', $string);
source: How to strip all spaces out of a string in php?
You can replace characters using SQL ->
SELECT REPLACE(caption,'\"','\'') FROM ...
u can try this
'".str_replace(' ', '', $messageTo)."'
and same for other
$sql = 'INSERT INTO messages " .
. "(username,usernameto,message_content,message_time) " .
. "VALUES ("'.$username.'","'.$messageTo.'",REPLACE("'.$message.','' '','''')",'.$time .')';
$result = mysql_query($sql, $cn) or die(mysql_error($cn));
I have a script I have used for quite a while that validates UK postcodes, issue I have is one part of the function uses ereg rather than preg_match.
Could someone point me in the right direction with the below please? I have added delimiters but not getting the same results as I was before.
$alpha1 = "[abcdefghijklmnoprstuwyz]";
$alpha2 = "[abcdefghklmnopqrstuvwxy]";
$alpha3 = "[abcdefghjkstuw]";
$alpha4 = "[abehmnprvwxy]";
$alpha5 = "[abdefghjlnpqrstuwxyz]";
// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
$pcexp[0] = '/^(' . $alpha1 . '{1}' . $alpha2 . '{0,1}[0-9]{1,2})([0-9]{1}' . $alpha5 . '{2})$/';
// Expression for postcodes: ANA NAA
$pcexp[1] = '/^(' . $alpha1 . '{1}[0-9]{1}' . $alpha3 . '{1})([0-9]{1}' . $alpha5 . '{2})$/';
// Expression for postcodes: AANA NAA
$pcexp[2] = '/^(' . $alpha1 . '{1}' . $alpha2 . '[0-9]{1}' . $alpha4 . ')([0-9]{1}' . $alpha5 . '{2})$/';
// Exception for the special postcode GIR 0AA
$pcexp[3] = '/^(gir)(0aa)$/';
// Standard BFPO numbers
$pcexp[4] = '/^(bfpo)([0-9]{1,4})$/';
// c/o BFPO numbers
$pcexp[5] = '/^(bfpo)(c\/o[0-9]{1,3})$/';
At a first glance, everything looks OK, but you need to make the regex case-insensitive by adding an i after the closing delimiter:
$pcexp[4] = '/^(bfpo)([0-9]{1,4})$/i';
and so on.
Your regexes don't allow for spaces between the parts of the postcode. Is this intentional? If not, add a \s* everywhere spaces are legal
Also, you can drop all instances of {1} and replace each {0,1} with ?.