My regex strips all points away except the last one. Great!
But what I wish, is only to strip "thousand" separators, not the "float" ones. I suppose floats ends with one or two digits.
I cannot change the input format.
Any idea how to extend that regex?
<?php
print_r(preg_replace('/\.(?=.*\.)/', '', "1.234.20")); // works
echo "<hr />";
print_r(preg_replace('/\.(?=.*\.)/', '', "1234.20")); // works
echo "<hr />";
print_r(preg_replace('/\.(?=.*\.)/', '', "1234.2")); // works
echo "<hr />";
print_r(preg_replace('/\.(?=.*\.)/', '', "1234.217")); // works
echo "<hr />";
print_r(preg_replace('/\.(?=.*\.)/', '', "1.000.234.20")); // works
echo "<hr />";
print_r(preg_replace('/\.(?=.*\.)/', '', "1.234")); // buuuh, 1234 would be correct
?>
If you assume every thousand separator is followed by exactly 3 digits, you can do it this way.
This excludes the case where a float is ending with 3 or more digits...
<?php
print_r(preg_replace('/\.([0-9]{3})/', '$1', "1.234.20")); // 1234.20
echo "<hr />";
print_r(preg_replace('/\.([0-9]{3})/', '$1', "1234.20")); // 1234.20
echo "<hr />";
print_r(preg_replace('/\.([0-9]{3})/', '$1', "1234.2")); // 1234.2
echo "<hr />";
print_r(preg_replace('/\.([0-9]{3})/', '$1', "1234.217")); // 1234217
echo "<hr />";
print_r(preg_replace('/\.([0-9]{3})/', '$1', "1.000.234.20")); // 1000234.20
echo "<hr />";
print_r(preg_replace('/\.([0-9]{3})/', '$1', "1.234")); // 1234
?>
Related
Let's say I have a URL: https://somesite.com/0/posts/20/290/755653-Title.html How would I get these variables: /0/, /20/, /290/ ? Note they are variables, they will always be different.
I thought I could get them like so:
$url = '//somesite.com/0/posts/20/290/755653-Title.html';
var_dump(parse_url($url));
but the array doesn't show them as separate variables. Should it be done with a preg_replace instead? I don't think I know how. Thank you for your help.
One option is to use a positive lookahead with preg_match_all where you capture the pattern in a capturing group:
(?=(/\d+/))
That will match
(?= Positive lookahead, assert what is directly on the right is
(/\d+/) Match /, 1+ digits and /
) Close positive lookahead
Regex demo | Php demo
For example
$re = '~(?=(/\d+/))~m';
$str = 'https://somesite.com/0/posts/20/290/755653-Title.html';
preg_match_all($re, $str, $matches);
print_r($matches[1]);
Result
Array
(
[0] => /0/
[1] => /20/
[2] => /290/
)
If you want to get the digits only without the surrounding slashes you could add the group around the digits only
(?=/(\d+)/)
Php demo
You could use explode() and turn the string into an array divided by the "/" delimiter.
<?php
// Example 1
$url = "https://somesite.com/0/posts/20/290/755653-Title.html";
$pieces = explode("/", $url);
echo $pieces[0] . "<br />";
echo $pieces[1] . "<br />";
echo $pieces[2] . "<br />";
echo $pieces[3] . "<br />";
echo $pieces[4] . "<br />";
echo $pieces[5] . "<br />";
echo $pieces[5] . "<br />";
echo $pieces[6] . "<br />";
echo $pieces[7] . "<br />";
echo "<hr />";
// Example 2
$data = "https://somesite.com/0/posts/20/290/755653-Title.html";
list($first, $second, $third, $fourth, $fifth, $sixth, $seventh, $eighth) = explode("/", $url);
echo $first . "<br />";
echo $second . "<br />";
echo $third . "<br />";
echo $fourth . "<br />";
echo $fifth . "<br />";
echo $sixth . "<br />";
echo $seventh . "<br />";
echo $eighth . "<br />";
?>
Output:
https:
somesite.com
0
posts
20
20
290
755653-Title.html
https:
somesite.com
0
posts
20
290
755653-Title.html
We can try splitting on path separator, and then using array_filter with an inline function to retain only purely numerical components:
$str = 'https://somesite.com/0/posts/20/290/755653-Title.html';
$parts = explode("/", $str);
$parts = array_filter($parts, function($item) { return is_numeric($item); });
print_r($parts);
This prints:
Array
(
[3] => 0
[5] => 20
[6] => 290
)
Note that this approach completely avoids the use of a formal regex, which might have performance implications if you needed to do this often in your script.
How do I echo the 4 at the bottom?
$request_body = '{"id":8801236,"order_id":"1854071","accepted":true,"type":"Payment","text_on_statement":null,"branding_id":null,"variables":{},"currency":"USD","state":"pending","operations":
[{"id":1,"type":"authorize","amount":8996,"pending":false,"qp_status_code":"20000","qp_status_msg":"Approved","aq_status_code":"000","aq_status_msg":"Approved","data":
{},"callback_url":"http://www.mywebsite/callback.php","callback_success":true,"callback_response_code":"200","created_at":"2015-11-11T13:32:22+00:00"},
{"id":2,"type":"capture","amount":8863,"pending":true,"qp_status_code":null,"qp_status_msg":null,"aq_status_code":null,"aq_status_msg":null,"data":
{},"callback_url":null,"callback_success":null,"callback_response_code":null,"created_at":"2015-11-11T14:37:18+00:00"}],"metadata":
{"type":"card","brand":"visa","last4":"0008","exp_month":1,"exp_year":2019,"country":"US","is_3d_secure":false,"hash":"fdsfsdfsdf4ds65f4dsf65ds4"
,"number":null,"customer_ip":"8.1.1.21","customer_country":"US","fraud_suspected":false,"fraud_remarks":
[]},"link":null,"shipping_address":null,"invoice_address":null,"test_mode":true,"acquirer":"nets","facilitator":null,"created_at":"2015-11-11T13:32:13Z","balance":0}';
$request_array = json_decode($request_body, TRUE);
echo $request_array['qp_status_code']."<br />";
echo $request_array['qp_status_msg']."<br />";
echo $request_array['aq_status_code']."<br />";
echo $request_array['aq_status_msg']."<br />";
I have tried to do a print_r on the request_array, but honestly that only confuses me more. I simply can not see what array these variable lies within. I have tried to call them with both variables and operations but alas.
Your JSON feed has more than one "operations" which means you can call them like this (to get the first only):
echo $request_array['operations'][0]['qp_status_code']."<br />";
echo $request_array['operations'][0]['qp_status_msg']."<br />";
echo $request_array['operations'][0]['aq_status_code']."<br />";
echo $request_array['operations'][0]['aq_status_msg']."<br />";
Or if you need all of them you need to loop in it:
foreach ($request_array['operations'] as $operation) {
echo $operation['aq_status_msg']."<br />";
}
Could someone assist me in helping me display more than 1/2 results?
Here is my code:
$url = "http://otter.topsy.com/search.json?q=debt%20management&window=a&perpage=10";
$jsonfile = file_get_contents($url);
$obj = json_decode($jsonfile);
foreach($obj as $result) {
echo $obj->response->list[0]->trackback_permalink;
echo "<br />";
echo $obj->response->list[0]->trackback_author_nick;
echo "<br />";
echo $obj->response->list[0]->content;
echo "<br /><br />";
}
?>
*Note: I have taken out my API key.
Using that code it shows two of the same results.
Anyone got a solution?
You iterate over $obj which is the top-level object containing two elements (request and response). Since you probably want to iterate over the response list, this is what you need:
foreach($obj->response->list as $result) {
echo $result->trackback_permalink;
echo "<br />";
echo $result->trackback_author_nick;
echo "<br />";
echo $result->content;
echo "<br /><br />";
}
Ah, just saw it:
remove the $obj++ ! You increment twice during each loop run. Once by the foreach() loop iterating itself, and once by manually iterating.
I am currently using the below code to echo a few different variables and 2 line breaks.
But what I would like to know is how can I echo all of the variables including line breaks into one line of code?
<?php
function findworld($var) {
return strpos($var, "world");
}
$firstvar = "hello world";
$secondvar = findworld($firstvar);
$thirdvar = strlen($firstvar);
echo $firstvar;
echo "<br />";
echo $secondvar;
echo "<br />";
echo $thirdvar;
?>
the concat operator in php is "."
echo $firstvar . "<br />" . $secondvar . "<br />" . $thirdvar;
http://www.php.net/manual/en/language.operators.string.php
You can pass multiple parameters to echo, separated by a comma:
echo $firstvar, "<br />", $secondvar, "<br />", $thirdvar;
To avoid repeating the line break, you could also use implode:
$firstvar = "hello world";
$values = array($firstvar,
findworld($firstvar),
strlen($firstvar));
echo implode('<br />', $values);
You can use string concatenation:
echo $firstvar . "<br />" . $secondvar . "<br />" . $thirdvar;
Like others have said, but with speech marks in the all the correct places ;)
echo $firstvar.'<br />'.$secondvar.'<br />'.$thirdvar;
You don't need to concatenate at all with double quotes, you can just:
echo "$firstvar<br />$secondvar<br />$thirdvar";
Both values are '2' but I am not getting a true. Why?
echo $getuser. "<br />";
echo $userurl. "<br />";
if ($getuser == $userurl) {
echo "true <br />";
}
Result
2
2
Instead of echoing them out, use var_dump() to see exactly what is stored in those variables:
var_dump($getuser); echo "<br />";
var_dump($userurl); echo "<br />";
You probably have some stray spaces or other characters which are not easy, or maybe not possible to visually detect.
use
var_dump($getuser);
var_dump($userurl);
Pay attention to the string length. Consider using trim() if needed
may be contain dummy space in that, use like this
echo $getuser. "<br />";
echo $userurl. "<br />";
if (trim($getuser) == trim($userurl)) {
echo "true <br />";
}
When I try
$getuser = 2;
$userurl = 2;
echo $getuser. "<br />";
echo $userurl. "<br />";
if ($getuser == $userurl) {
echo "true <br />";
}
Results:
2
2
true
Works for me. On XAMPP (Windows) and Linux (Apache) alike. So there's problem probably somewhere in configuration or something...