I have a php code as shown below in which at Line A prints the following o/p:
php code:
<?php
public function record_insert($data, $type)
{
echo '<pre>'; print_r($data); echo '</pre>'; // Line A
return false;
}
?>
O/P:
Array
(
[hello] => Good Morning
[world] => Good Evening
[text] => No
)
The top 2 array fields value have http://google.com/ and https://www.bing.com/ as url.
Problem Statement:
What I want to achieve is, I want to get rid of hyperlinks from the top 2 fields value.
I know I have to use strip_tags() function but I am not sure how I have to use it.
strip_tags() function is used to remove any HTML or XML tags from a string.
function record_insert($data, $type = "")
{
// remove hyperlinks from the top 2 fields
$data['hello'] = strip_tags($data['hello']);
$data['world'] = strip_tags($data['world']);
echo '<pre>';
print_r($data);
echo '</pre>';
return false;
}
$data = array(
"hello" => "<a href='http://google.com/'></a>",
"world" => "<a href='http://bing.com/'>Good Evening</a>",
"text" => "No"
);
record_insert($data);
O/P
Array
(
[hello] =>
[world] => Good Evening
[text] => No
)
Related
I am having problems echoing array data from a nested array, my array (for a single order) looks like this...
Array
(
[buyer_name] => Mr Smith
[buyer_phone] => 01234 567890
[buyer_email] => mygreatemail#fakeemail.com
[items] => Array
(
[0] => Array
(
[quantity] => 3
[item_title] => title 1
[custom_label] => JD3433
[item_variation] => Variation detail for title 1
[price] => £8.00
)
[1] => Array
(
[quantity] => 1
[item_title] => title 2
[custom_label] => JD5544
[item_variation] => Variation detail for title 2
[price] => £7.00
)
)
)
Generated from this code...
function readCSV($csvFile)
{
$output = [];
$fileHandle = fopen($csvFile, 'r');
$header = fgetcsv($fileHandle);
while (!feof($fileHandle)) {
$fileRow = fgetcsv($fileHandle, 1024);
$orderId = $fileRow[0];
// skip this row if it's empty (the first field contains no id)
if (empty($orderId)) {
continue;
}
/*
$fileRow[3] is "Buyer name", the first field that's present in one type of row
(the one containing common properties of the order). By checking if it's empty,
we identify the contents of the row - not empty means order row with common
properties, empty means item row with specific item properties.
*/
if (!empty($fileRow[3])) {
// no need to repeat the id inside the array - it's already stored in the key
$output[$orderId] = [
'sales_record' => $fileRow[0],
'order_number' => $fileRow[1],
'buyer_username' => $fileRow[2],
'buyer_name' => $fileRow[3],
// here you can continue explicitly adding any property you need
];
} else {
// add a new item entry
$output[$orderId]['items'][] = [
'item_number' => $fileRow[20],
'item_title' => $fileRow[21],
'quantity' => $fileRow[24],
'price' => $fileRow[25],
// here you can continue explicitly adding any property you need
];
}
}
fclose($fileHandle);
return $output;
}
I can echo individual main array data without issue using..
foreach($csv as $order) {
echo "Sales Record: "; echo $order[sales_record];
}
But having problems trying to echo nested array data.
I have read many similar questions on here and tried..
foreach($csv[$orderId]['items'] as $orderitems) {
echo $orderitems; echo "<br>";
}
with no luck, what am I doing wrong, please?
EDIT TO ADD...
If I can't echo the array in that way then this would result in me using lots of "if" statements (there could be 100+ items in an order) and the below is just for an example for "title", I'd have to repeat several hundred times for each item in the nested array, there must be a better way to achieve this??
if ($order[items][0][item_title] != "") { echo $order[items][0][item_title]; echo "<br>"; } else {}
if ($order[items][1][item_title] != "") { echo $order[items][1][item_title]; echo "<br>"; } else {}
if ($order[items][2][item_title] != "") { echo $order[items][2][item_title]; echo "<br>"; } else {}
//and so on
First, array-keys must be in Quotation marks:
no: $order[items][0][item_title]
yes: $order['items'][0]['item_title']
Normally your PHP version throws a warning:
Warning: Use of undefined constant item_title - assumed 'item_title' (this will throw an Error in a future version of PHP)
You can iterate over the array:
foreach($order['items'] as $item) {
echo $item['item_title']."<br/>";
}
If you have a nested array (array of orders and each order has an array of items) you can nest the foreach.
$orders = readCSV('filename.csv');
foreach ($orders as $orderId=>$order) {
foreach($order['items'] as $item) {
if (!empty($item['item_title'])) echo $item['item_title']."<br/>";
}
}
. concat two strings. You don't Need two echo-commands.
Pay attention to the wording, that makes the code more readable.
foreach (<plural> as <singular>)
I have an array which I create. When reading the array with print_r it is not returning with the correct data inputted! I am missing specific sectionsn such as < & > brackets with its headings.
How can i preserve these?
Code:
$params = array(
"Parm1" => "test",
"Parm2" => "hi",
"Parm3" => GUID(),
"Parm4" => "lol",
"Parm5" => "
<R>
<R1>the</R1>
<R2>dog</R2>
<R3>is</R3>
<R15>happy</R15>
<R20>today</R20>
</R>
");
Basically the only data that is jumbled up is the Parm5 section. I want everything inside to return exactly as it is! EG: Reading as is i only receive Array ( [Parm1] => test [Parm2] => hi [Parm3] => B18BE727-8F79-4D4A-80EA-3974B1429F78 [Parm4] => lol [Parm5] => the dog is happy today ) from print_r
I want to return:
Array ( [Parm1] => test [Parm2] => hi [Parm3] => B18BE727-8F79-4D4A-80EA-3974B1429F78 [Parm4] => lol [Parm5] => <R><R1>the</R1> <R2>dog</R2> <R3>is</R3> <R15>happy</R15> <R20>today</R20></R> )
escape param5 with htmlspecialchars('<R> ..... </R>'). Your browser currently sees it as html tags and parses it.
Use htmlspecialchars on the return value of print_r:
echo "<pre>";
echo htmlspecialchars(print_r($params, true));
echo "</pre>";
I am getting a response from database in array like this:
Array ( [mage_form_post_status] => draft [mage_form_post_type] => post [mage_form_post_permission] => public [mage_form_post_author] => 1 [mage_form_post_redirect] => 0 )
Array ( [mage_form_post_status] => draft [mage_form_post_type] => post [mage_form_post_permission] => public [mage_form_post_author] => 1 [mage_form_post_redirect] => 88 )
Array ( [mage_gym_name] => Fanna - test [mage_your_name] => John )
What I am trying to achieve is to display mage_gym_name value only.
I have succeed to display it with this code:
foreach($gym_titles as $gym_title){
print_r(unserialize($gym_title));
echo '<br><br><br>';
}
Problem is that when there is no result, like in first two arrays I get 3 empty breaklines.
So I believe that I should wrap:
print_r(unserialize($gym_title));
echo '<br><br><br>';
In some kind of condition which should check if that current array contains that key name.
I have tried like this but it doesn't work:
foreach($gym_titles as $gym_title){
if (array_key_exists('mage_gym_name', $gym_title) {
echo "The 'first' element is in the array";
print_r(unserialize($gym_title));
echo '<br><br><br>';
} else {
echo 'no such element in this array';
}
}
You need to unserialize() before you check for the key:
foreach($gym_titles as $gym_title){
$result = unserialize($gym_title);
if(isset($result['mage_gym_name'])) {
echo "The 'first' element is in the array";
print_r($result);
echo '<br><br><br>';
} else {
echo 'no such element in this array';
}
}
I use isset(), feel free to use array_key_exists() if you must.
I have a strange issue and could not find the reason.
I have url
http://example.com/cp/user_detail?userID=2
So If I print this code
print_r($_REQUST);
it should not print
Array ( [userID] => 2 ...... ?
But its printing this array
Array ( [userID] => 84ac17a3690b4ecd8c8abfba8687e750 [_pk_id_2_2fa0] =>
26c324a269691d77.1410515405.1.1410515405.1410515405. [__utma] =>
24293118.939351632.1410515405.1410515405.1410515405.1 [__utmz] =>
24293118.1410515405.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
[PHPSESSID] => 0394d01235809edd26422eedc400b6b5 )
Is it not strange?
I have a general function for getting get or post value
function chf($value)
{
if(isset($_REQUEST[$value]))
{
if(isset($_POST[$value]))
{
$value=$_POST[$value];
}
else
{
$value=$_REQUEST[$value];
}
$keywords=array();
$keywords=array('update','delete','select');
foreach($keywords as $key=>$val)
{
$value= str_replace($val,'',$value);
}
return $value;
}
else
{
return '';
}
}
How should I changed it so it gives me correct string values?
$_REQUEST includes cookies by default: http://php.net/manual/en/reserved.variables.request.php . The PHPSESSID, __utm* are cookies. If you want just the URL parameters use $_GET.
If you want data from your URL you should use
print_r($_GET);
This wil give you an array like:
Array(
[userID] => 2
)
I have following code to match preg_match function to make post clean.###
foreach ( $_POST as $key => $value) {
if (preg_match("~^[\r\n\s\w\d /<>:,.?#;-]+$~", $value)) {
echo 'Correct';
} else {
echo 'Incorrect';
}
}
I have an POST array in PHP, where some fields may be blank/empty. When I leave some fields blank it gives an error literally here says incorrect instead of accepting. I want them to pass through preg_match even some fields are empty.
I do have following as print_r of POST where some fields are empty
Array {
[email] => mi#mi2.com
[facebook] =>
[twitter] =>
[gplus] => glus
[firstname] => mi2
}
<urregex>|(^$)
Try this.See demo.
http://regex101.com/r/oO8zI4/1