Match an actual dollar symbol in php regex - php

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)

Related

Add space between words not line breaks in php

Hi straight forward question really. I have a search function in php that prints out the required information from a data base. But it prints it out as one word. I don't want a line break...just a space between words. I've googled and checked this forum for answers but can't seem to find any.
The code works and does as it is required but it doesn't look neat.
Instead of: ID Job Title Job Description Job location Job Category
it looks like this:
IDJobTitleJobDescriptionJoblocationJobCategory
This is part of my php code.
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo
'<p>'
. $results['id']
. $results['job_title']
. $results['job_description']
. $results['job_location']
. $results['job_category']
. '</p>';
Please note I want it in one line, not line breaks. Thanks.
You have to echo the space like this .' '.
Or in your Case just replace your code with this.
echo
'<p>'
. $results['id']
.' '. $results['job_title']
.' '. $results['job_description']
.' '. $results['job_location']
.' '. $results['job_category']
.' '. '</p>
echo '<p>'
.$results['id'] . ' '
. $results['job_title'] . ' '
. $results['job_description'] . ' '
. $results['job_location'] . ' '
. $results['job_category'] . ' '
. '</p>';

Getting a Syntax error, but I can't find the syntax error

So I'm working on an editor for a friend of mine, and I'm getting a strange Syntax error. It's strange because I'm currently creating an NPC editor using the shell of the Item editor I made a while back. That's saying I literally just changed the variables and changed everything that said 'item' to 'npc'. However, I'm getting a syntax error at a random column and I can't find out what the error is. It's in the editing section of the editor(lol). The delete and create parts of the editor work fine.
}else if($state == "edit")
{
$editsql = "UPDATE npcs SET name='" . $name . "', description='" . $description . "', gender=" . $gender . ", size=" . $size . ", dialog='" . $dialog . "', hair_style=" . $hair_style . ", hat=" . $hat . ", top=" . $top . ", bottom=" . $bottom . ", movement_pattern=" . $movement_pattern . ", behavior=" . $behavior . ", range=" . $range . ", uses_special_pokemon=" . $uses_special_pokemon . ", pokemon_1=" . $pokemon_1 . ", pokemon_2=" . $pokemon_2 . ", pokemon_3=" . $pokemon_3 . ", pokemon_4=" . $pokemon_4 . ", pokemon_5=" . $pokemon_5 . ", pokemon_6=" . $pokemon_6 . " WHERE id=" . $id;
this is the error:
Could not edit npc ID 3 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'range=0, uses_special_pokemon=0, pokemon_1=1, pokemon_2=1, pokemon_3=1, pokemon_' at line 1
I can't quite figure out what it's calling out near 'range' and range itself looks fine to me, so I don't see an error at all. It's most likely something completely obvious that I'm just overlooking as usual, but I'm stumped.
You'll want to rename range to range_, because Range is a SQL reserved word. You could enclose it in backticks, which are different than single quotes. ` VS ' ...
If you seperate the query into multiple lines your error message will tell you where it failed closer to where the actual error was. It's a one-liner, so it tells you error exists on line 1. Typically, seperate clauses, i.e.
select xxxx
from yyyy
where xxxx = zzzz
then you'll know it's an error in syntax and in what clause.

PHP reg exp - where did DOI go?

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);

Making a HTML Tagged email in php

I want to create an HTML Message to send an email in PHP.
$message = $mess0 . "</br>" . $mess1 . "</br>" . $mess2 . "</br>" . $mes1 . "</br></br>" . $mes2 . "</br>" . $mes23 . "</br></br>" . $mes3 . "</br></br>" . $mes4 . "</br>" . $mes5 . "</br>" . $mes6 . "</br>" . $mes7 . "</br>" . $mes8 . "</br>" . $mes9 . "</br></br>" . $mes10 ;
$message = <html><body><p>$message</p></body></html>;
Here are some variables.
I am getting the following error.
Parse error: syntax error, unexpected '<' in /home/thevowaa/public_html/iphoneapp/webservice/file.php on line 214
Add HTML tags between double quotes.
$message = "<html><body><p>".$message."</p></body></html>";
Where are the double quotes see below
$message = "<html><body><p>$message</p></body></html>";
There are two possible ways to hold HTML in PHP variable. You can use single quote or double quotes. You also need to put a dot(.) before and after single/double quotes. Your PHP string could be constructed in following two ways:
$message = '<html><body><p>'.$message.'</p></body></html>';
or like this,
$message = "<html><body><p>".$message."</p></body></html>";
Also, use of single quotes(') is encouraged in PHP coding because it's doesn't clash with javascript or css double quotes(") when constructing html pages using PHP.
For more information on usage of quotes in PHP, check out this stackoverflow answer

html entities and angle brace issue

So, I have this rel/URL I am trying to stuff into a variable so I can print it out elsewhere:
$relnext = "<link rel='next'
href='javascript:".$content_pager->PagerName
. "_form."
. $content_pager->PagerName
. "PagerPage.value=\""
. $content_pager->Page+1
. "\"; "
. $content_pager->PagerName
. "DoSubmit();' />";
As it is, when I print it out, all I get is:
1"; MediaBoxContentDoSubmit();' />
After some research it 'appears' that I should use htmlentities, but:
echo htmlentities($relnext);
also just produces:
1"; MediaBoxContentDoSubmit();' />
Is there some other function that should be used here?
Thanks very much for any help you can give!
You have an operator precedence / associativity problem. The . operator to the left of the + operator execute before the + operator, because they are all left-associative and have the same precedence. You want the + operator (in $content_pager->Page+1) to execute first, then all the . operators.
As it is, you are adding a string to a number (1) with the + operator, in which case the string (everything before the +) will be taken as 0. That is why the first character is a 1, because it is the result of "some string"+1, which is interpreted as 0+1.
So, your first snippet should be:
$relnext = "<link rel='next' href='javascript: " . $content_pager->PagerName . "_form." . $content_pager->PagerName . "PagerPage.value=\"" . ($content_pager->Page+1) . "\"; " . $content_pager->PagerName . "DoSubmit();' />";
Note that the $content_pager->Page+1 part is now in parentheses.
More information:
http://php.net/manual/en/language.operators.precedence.php

Categories