I am working on a Wordpress website and I want to get some order details right after the submission. One of them, returned with a long string which I want to split and format it as I need. Here is the example:
echo $order->get_formatted_shipping_address();
My name<br />My address<br />My postcode
I try to split the string in the <br /> to use each of the fields in a different place. I tried like this:
$data = explode("<br />", $order->get_formatted_shipping_address());
$name = $data[0];
$address = $data[1];
$post = $data[2];
However, the <br /> keeps staying in the string and the $name has the whole string as in $data.
Any idea?
Tried the following on WooCommerce 2.6.1
$data = explode('<br/>',$order->get_formatted_shipping_address());
Note that the delimiter is <br/> and not as you used it <br />, yours has an extra space.
As an alternative, instead of explode you should use the magic properties from the class WC_Abstract_Order.
Example
print $order->shipping_first_name;
print $order->shipping_address_1;
print $order->shipping_postcode;
This will output the fields without going through the implode that happens inside get_formatted_shipping_address()
Your code should work, I even tested it on my machine. Put this code in some new file and just test it to see if it gives the wanted result. If it does, then you're making a mistake on the place. Are you sure you are using the right variables?
<?php
class wp {
public function get_formatted_shipping_address() {
return "My name<br />My address<br />My postcode";
}
}
$order = new wp;
$data = explode("<br />", $order->get_formatted_shipping_address());
print_r($data);
$name = $data[0];
$address = $data[1];
$post = $data[2];
Related
I want to echo only the first word of a url parameter. For example: /?name=Mike%20Jackson. I want only "Mike". How is that done?
I can get the whole parameter.
<?php // show welcome back message if coming from the waiting list email
$name = '';
if (isset($_GET["name"]))
{
$fullname = $_GET["name"];
}
?>
I expect the first word only.
To break the string at a space, you would use the explode function. That separates the parameter and breaks it into an array, so to get the first word you just need to get the 0th index:
$fullname = explode(" ", $_GET["name"])[0];
If space in your URL is encoded as in your example, you just need to replace the space with the encoding:
$fullname = explode("%20", $_GET["name"])[0];
Edit: As pointed out in the comments, the first method should be the way to go because your method of getting the parameter automatically decodes the URL.
I want to integrate SMS to my API and try to pass the variables in URL but it says error, no number found.
Please help
$sender = "9999708993";
$reply_message = "This is an auto generated message";
echo $sender;
echo $reply_message;
$lines = file_get_contents('http://sms2.oxyzen.in/httpapi/sendsms.php?loginid=myid&password=mypassword&senderid=mysenderid&message=$reply_message&number=$sender');
echo "<pre>$lines</pre>";
use double quotes rather than single quotes if you want to put variables in:
$lines = file_get_contents("http://sms2.oxyzen.in/httpapi/sendsms.php?loginid=myid&password=mypassword&senderid=mysenderid&message=$reply_message&number=$sender");
Please try this:
$sender = "9999708993";
$reply_message = urlencode("This is an auto generated message");
//I think you need to change these
$loginid='myid';
$mypassword='mypassword';
$mysenderid='mysenderid';
$uri = "http://sms2.oxyzen.in/httpapi/sendsms.php?loginid=$myid&password=$mypassword&senderid=$mysenderid&message=$reply_message&number=$sender";
var_dump($uri);
$lines=file_get_contents($uri);
var_dump($lines);
try cleaning the URL, it might have a new line at the end
trim("$URL");
I have the below feed value
<item>
<description><strong>Contact Number:</strong> +91-00-000-000<br /><br /><strong>Rate:</strong> xx.xx<br /><br /><strong>Fees and Comments:<br /></strong><ul><li>$0 fees</li><li>Indicative Exchange Rate</li></description>
</item>
Now i wanna get Contact number and rate as well as Fees and comments in separte value.
how can i get this value ..any one????
Description
You should probably read this with a parsing engine. however if your use case is this simple then this regex will:
capture each of the fields
allow the fields to appear in any order
^(?=.*?Contact\sNumber:<\/strong>([^<]*))(?=.*?Rate:<\/strong>([^<]*))(?=.*?Fees\sand\sComments:.*?<li>([^<]*)<.*?<li>([^<]*)<)
Live Example: http://www.rubular.com/r/j0aStij3L8
It kind of depends on what reliable patterns there are to the rest of your feed (or future feeds). It doesn't look like an XML parser is going to work here as the example doesn't look like well formed XML.
A good way to start is using explode to split the string into an array of strings, it looks like is a good delimiter to split on. So this would look like:
$split_feed = explode("<br />",$feed);
where $feed is your feed input in the question, and $split_feed will be your output array.
Then, from that split feed, you can use strpos (or stripos) to test for keys in your string, to determine which field it references, and replace to get the value out of the key/value string.
I think this is you want
<?php
$value = '<strong>Contact Number:</strong> +91-00-000-000<br /><br />
<strong>Rate:</strong> xx.xx<br /><br />
<strong>Fees and Comments:<br /></strong><ul><li>$0 fees</li>
<li>Indicative Exchange Rate</li>';
$steps = explode('<br /><br />', $value);
$step_2_for_contact_number = explode('</strong>', $steps[0]);
$contact_number = $step_2_for_contact_number[1];
$step_for_rate = explode('</strong>', $steps[1]);
$rate = $step_for_rate[1];
$feed_n_comment_s_1 = explode('</li>', $steps[2]);
$feed_n_comment_s_2 = explode('<li>', $feed_n_comment_s_1[0]);
$feed_n_comment = $feed_n_comment_s_2[1];
echo $contact_number;
echo "<br/>";
echo $rate;
echo "<br/>";
echo $feed_n_comment;
?>
You can also have a look at this pattern: (uses named groups)
(?<key>[a-zA-Z\d\s]+)(?=\:).*?\>(?<value>[^<]+)
Live Demo
Im trying to strip find_loc= and &cflt=pizza I got the majority stripped its just these last 2 things and whenever I try to use trim it doesn't delete it out it keeps saying array even when i try to print it, it says array.
<?php
$foo = 'http://www.yelp.com/search?find_loc=2190+W+Washington+Blvd%2C+Los+Angeles+90018&cflt=pizza ';
$blah = parse_url($foo);
$blah[query];
//the code above echos out find_loc=2190+W+Washington+Blvd%2C+Los+Angeles+90018&cflt=pizza
$thids = trim(''.$blah.'','find_loc=');
echo $thids;
?>
$thids = str_replace(array('&cflt=pizza','find_loc='), '', $blah);
parse_str($blah['query'], $query_vars); // decompose query string into components
unset($query_vars['find_loc']); // delete this particular query variable/value
unset($query_vars['cflt']);
$blah['query'] = http_build_query($query_vars); // rebuild the query string
$foo = http_build_url($blah); // rebuild the url
I'm having trouble describing this issue, which is probably why I can't find the answer on google.. so I figured I would try getting help here. If I'm repeating this question, feel free to direct me to a link to the thread.
So basically the issue I'm having is I am trying to pass a variable to a function that contains some php code to be eval'd.
Here's the simplified version of the code:
function senduser($body) {
$query = mysql_query("SELECT * FROM User_tbl");
while ($row = mysql_fetch_array($query)) {
echo eval($body);
}
}
$body = 'Hello $row[\'user_first_name\'] <br>';
sendUser($body);
--
For some reason, the output isn't putting out what I want. I've gotten a few whitespace errors, and a few times I've gotten the code to output the plain text of the variable $body.
Any help is appreciated. Let me know if I need to clarify the issue further.
I would change it to this:
function sendUser($body) {
$query = mysql_query("SELECT * FROM User_tbl");
while ($row = mysql_fetch_array($query)) {
echo $body($row);
}
}
And then call it like this (php 5.3+):
$body = function ($row) {
return "Hello ".
htmlspecialchars($row['user_first_name'], ENT_QUOTES, 'UTF-8').
"<br />";
};
sendUser($body);
In php <= 5.2, it's a lot messier:
$body = create_function(
'$row',
'return "Hello ".'.
'htmlspecialchars($row["user_first_name"], ENT_QUOTES, "UTF-8").'.
'"<br />";'
);
sendUser($body);
That isn't now eval works; it returns null unless you explicitly return a value. You're also missing quotes around the string, and a semicolon at the end of the statement.
To get it to echo something, you'd have to pass the echo as part of the code to be evaluated:
$body = 'echo "Hello $row[\'user_first_name\'] <br>";';
or, to get your code working as written, you'd have to return the formatted string:
$body = 'return "Hello $row[\'user_first_name\'] <br>";';
This is a pretty contrived use of eval. You'd be far better off passing in a printf-style format string and using sprintf to substitute values into it, and returning that string for printing. As it stands you seem to be mixing your display logic with your database logic, which is a bad thing.
Your code, as is, will never work. Removing the mysql portion:
<?php
function senduser($body) {
$row['user_first_name'] = 'Fred';
echo eval($body);
}
$body = 'Hello $row[\'user_first_name\'] <br>';
sendUser($body);
Gives me:
PHP Parse error: syntax error, unexpected T_VARIABLE in /home/marc/z.php(5) : eval()'d code on line 1
Anything you pass in to eval() must be raw PHP code. It can't be plaintext with embedded <?php ?> PHP blocks - it has to be actual PHP code. When you fix up $body to account for this:
$body = 'echo "Hello {$row[\'user_first_name\']} <br>";';
Then you get:
Hello Fred <br>
I'm not exactly positive what you're trying to do, but I think your problem is in the definition of $body and your use of eval.
$body = 'Hello $row[\'user_first_name\'] <br>';
is not a valid line of php, and eval won't know what to do with it.
See if this fits what you want:
function senduser($body) {
$query = mysql_query("SELECT * FROM User_tbl");
while ($row = mysql_fetch_array($query)) {
eval($body);
}
}
$body = 'echo "Hello {$row[\'user_first_name\']} <br>";';
sendUser($body);