Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have this simple concatenation in my PHP code that concatenates a few strings together to make an XML request. For some reason, when I concatenate the customerId with the string chain, it's not parsed. But if I pass a constant number within single quotes, it's parsed. Here is an example.
I if I concatenate with a constant number, like this:
$out='<?xml version="1.0"?>';
$out=$out.'<soapenv:Envelope ';
$out=$out.'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ';
$out=$out.'xmlns:v1="http://www.test.com/v1">';
$out=$out.'<soapenv:Header/>';
$out=$out.'<soapenv:Body><v1:loadData><customerNumber>';
$out=$out.'2985634';
$out=$out.'</customerNumber></v1:loadData>';
$out=$out.'</soapenv:Body>';
$out=$out.'</soapenv:Envelope>';
Echoing $out will give me:
<?xml version="1.0"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.test.com/v1"><soapenv:Header/><soapenv:Body><v1:loadData><customerNumber>2985634</customerNumber><version>1</version></v1:loadData></soapenv:Body></soapenv:Envelope>
But if I pass a variable instead of a constant number(which is what I passed from a from by POST method), it wont' be parsed, as shown below:
$custId = $_POST['customerId'];
$out='<?xml version="1.0"?>';
$out=$out.'<soapenv:Envelope ';
$out=$out.'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ';
$out=$out.'xmlns:v1="http://www.test.com/v1">';
$out=$out.'<soapenv:Header/>';
$out=$out.'<soapenv:Body><v1:loadData><customerNumber>';
$out=$out.$custId;
$out=$out.'</customerNumber></v1:loadData>';
$out=$out.'</soapenv:Body>';
$out=$out.'</soapenv:Envelope>';
The output of echo will be:
<?xml version="1.0"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.test.com/v1"><soapenv:Header/><soapenv:Body><v1:loadData><customerNumber></customerNumber><version>1</version></v1:loadData></soapenv:Body></soapenv:Envelope>
I tried different ways of concatenation and stringifying the variable, but none worked :(
Your code is working without problems , here is the result in my browser for this code:
<?php
$_POST['id']=2985634;
$out='<?xml version="1.0"?>';
$out=$out.'<soapenv:Envelope ';
$out=$out.'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ';
$out=$out.'xmlns:v1="http://www.test.com/v1">';
$out=$out.'<soapenv:Header/>';
$out=$out.'<soapenv:Body><v1:loadData><customerNumber>';
$out=$out.$_POST['id'];
$out=$out.'</customerNumber></v1:loadData>';
$out=$out.'</soapenv:Body>';
$out=$out.'</soapenv:Envelope>';
echo $out;
?>
After echoing XML output, use view source to check the output result instead of just viewing on the browser.
Define $custId as a string.
$custId = '2985634';
Also, instead of using $out=$out.'string';, try using $out .= 'string';
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am trying to create a unique id by concatenating 3 variables using forward slashes(/). my code is like so
$year . "/" . $acronym . "/" . $num;
I am expecting an output of
"18/MC/1"
but the output I get is
"18\/MC\/1"
What am I doing wrong. I have already tried using stripslashes() but it doesn't do anything to the output.
You could use join function
<?php
// ...
join([$year, $acronym, $num], '/');
For more info, see: Join function documentation
I found what was wrong,
The code below outputs the correct format 18/MC/1
return response()->json([$id]);
while my previous code was
return response()->json($id);
which gave me an output of
`"18/MC/1"
There is function in php to remove backward slash from string use following function.
echo stripslashes(string);
output: 18/MC/1
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I'm using PHP to retrieve a document and find some data within the HTML.
I've used Tidy clean and repair as the document contains lots of bad html.
Anyway,
In the html document there is a tag like:
Link 12345
I want to get the value of the attribute (www.google.com) if the text content (Link 12345) matches a certain string.
$h2 = $doc->getElementsByTagName('a');
for ($i2; $i2 < $h2->length; $i2++) {
$attr2 = $h2->item($i2)->getAttribute('href');
if ($h2->item($i2)->textContent == "Link 12345")
print "FOUND";
}
which doesn't seem to work. I know that the for loop returns 'Link 12345' at some point (when ->textContent is called). But the comparison always fails even though Link 12345 appears if it is printed out. I suspect there is some issue with the encoding but I can't get it fixed.
Thanks.
You can use PHP's DOMXPath to execute an XPath query against your DOM object.
I believe that for yours it'll be
//a[text()="Link 12345"]
Will return all the who's text is "Link 12345".
A simple bug: you are testing "$h2->item($i2)->textContent" instead of "$h2->textContent"
Isn't it?
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm trying to find a text between $something and $something_else and ditch it out in an array.
I would think you need preg_match to do this but I have been trying alot and still have no idea.
This should work no matter what $something and $something_else is.
You need to read the documentation of preg_match and preg_match_all.
Here's a simple example that will match whatever content inside (double quotes)..
<?php
preg_match_all('~"(.*?)"~',
'Hey there "I will be matched", because "I am inside the double quotes"',
$out, PREG_PATTERN_ORDER);
print_r(($out[0]));
OUTPUT :
Array
(
[0] => "I will be matched"
[1] => "I am inside the double quotes"
)
Correct me if i am wrong. We can use explode one string into a array. Use pre_match_all for the another string with each word of the array . In this way, it will work with any string.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi, I have a problem when try access to $_POST vars in php. I have a combo with this name "c012". Well, I send the form with this var, and I have checked this var is send ok, and when I try access with this code, where $var1, $var2 and $var3 are numbers:
$var1 = 0;
$var2 = 1;
$var3 = 2;
$pointer_combo = "c".$var1.$var2.$var3;
echo $_POST['$pointer_combo'];
Don't show anything, but if I try this:
echo $_POST['c012'];
Works, and show the value. Whats the problem with code above?
If you are using a dynamic index (index value stored in a variable), you don't need the quotes.
Try this:
echo $_POST[$pointer_combo];
PHP won't do variable substitution if the value is in single quotes. Only double quotes or no quotes. So
echo $_POST[$pointer_combo];
Would work, as would:
echo $_POST["$pointer_combo"];
(But obviously in that second example there isn't much point in the quotes being there!)
Lose the quotes:
$_POST[$pointer_combo];
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
How would one use the mysql/php $list['key'] inside a php echo i.e
echo "<p> My Text . $list[''] . </p>";
Thanks In Advance
It is as simple as that:
echo "<p> My Text $list[index] </p>";
N. b.: you do not use single quotes (') for the index as you usually would, like in $a=$list['index'];, since the whole thing is already enclosed in double quotes (").
Correction:
Just found out, with indices like 'a b' you still do need the quotes! (Thanks, Jon!)
Edit: (response to comment)
That is a competely different thing! Use
list($var1,$var2) = mysql_fetch_assoc($result);
instead. The list()-construct (is it a function?!?) extracts the values out of the assigned array (in your case the result of your mysql_fetch_assoc()-function). Assuming, that your result set returns values for two columns (otherwise you will have to supply more variables in list()). And then place the variables into your text like
echo "<p> My Text $var1 and somewhere maybe also $var2 ... </p>";
Still, since you are using mysql_fetch_assoc($result) you could do
$z=mysql_fetch_assoc($result);
echo "<p> My Text $z[field1] and somewhere maybe also $z[field2] ... </p>";
with field1 and field2 being the actual column names from your MySQL table.
It is customary on this site now, to also warn you of the dangers of still using the deprecated mysql_*functions. You should change to the more secure and modern versions of mysqli_*...
In this context, usually you would just write the bare array key between the angle brackets:
echo "$array[key]";
However, if key is the empty string or if it contains any of a class of characters that have special meaning in this context then you can't do this. For example, these will not work:
echo "$array[]"; // empty string key
echo "$array[]]"; // string key: "]"
In this case you can add {} around the variable and use single quotes as if you were not inside a double quoted string context. For example:
echo "{$array['']}";
echo "{$array[']']}";
This is called complex string variable parsing; see the manual for more information.