I'm rather new to PHP and I am kind of stuck here writing this simple script; what I am trying to ultimately do is go through the content of a string and find the positions of all the occurrences I have listed in my $definitions array then map those positions in a separate array and return it...
rather simple but I am not sure where the problem arises, when i print_r on the array in different parts of code, thinking its a scope issue, I keep seeing that the key value of the array is NULL and also when I try and access a value of the array I am sure exists for a given key, i also get nothing; any help would be appreciated...
thank you!
<?php
class html2DokuWiki {
function definition_map($content){
$definitions = array("<title" => " ","<h" => array("=", 6),"<p" => "\n\n","<b" => "**","<strong" => "**","<em" => "//","<u" => "__","<img" => " ","<a" => " ","<ul" => " ","<ol" => "*","<li" => "-","<dl" => " ","<dt" => " ","<dd" => " ");
$element_pos = array();
foreach($definitions as $html_element){
$offset = 0;
$counter = 0;
$element_pos[(string)$html_element] = array(); //ask phil why do i need to cast in order to use the object?
while($offset = strpos($content, $html_element, $offset + 1)){
$element_pos[(string)$html_element][] = $offset;
};
};
//print_r($element_pos);
echo $element_pos["<p"][0];
return $element_pos;}
function run($page){
return $this->definition_map($page);}
};
$debug = new html2DokuWiki();
$url = "http://www.unixwiz.net/techtips/sql-injection.html";
$content = file_get_contents($url);
//echo $content;
//print_r($debug->run($content));
$test = $debug->run($content);
echo "<p> THIS:".$test["<p"][0]."</p>";
//print_r($test);
?>
If it's the key you want to use as $html_element as an index you should do:
foreach($definitions as $html_element => $value){
Related
I'm using a Sendinblue SMTP into my PHP project and I want to send transactionals emails to a dynamic list of emails, the problem is that I have a syntax error when I'm using a variable instead a string. For example, this code works great:
include 'Mailin.php';
$mailin = new Mailin('senders#sender.com', 'key');
$mailin->
addTo(
array(
'email1#email.com' => '', 'email2#email.com' => '', 'email3#email.com' => ''
)
)->
setFrom('sender#sender.com', 'Test')->
setReplyTo('sender#sender.com', 'Test')->
setSubject('Example')->
setText('Test')->
setHtml($htmlContent);
$res = $mailin->send();
print_r($res);
But if I use a variable instead the Strings in "addTo Array" it shows syntax error, for example:
$customers = '';
foreach ($clientes as $customer) {
for ($i=1; $i < 41; $i++) {
if ($customer['email'.$i] != "" or $customer['email'.$i] != NULL) {
$customers .= "'".$customer['email'.$i]. "' => '', " ; //for each customer's email add the email in " 'email#email.com' => '', " format
}
}
}
$customers = substr($customers, 0, -2); //removes last space and comma of the String
include 'Mailin.php';
$mailin = new Mailin('senders#sender.com', 'key');
$mailin->
addTo(
array(
$customers
)
)->
setFrom('sender#sender.com', 'Test')->
setReplyTo('sender#sender.com', 'Test')->
setSubject('Example')->
setText('Test')->
setHtml($htmlContent);
$res = $mailin->send();
print_r($res);
If I use the Print_r($customers) function it shows the exact string that I used in my first example, even if I use the code:
$text = "'email1#email.com' => '', 'email2#email.com' => '', 'email3#email.com' => ''";
if ($customers == $text) {
print_r("Yes");
}else{
print_r("No");
}
The result is "Yes", but when I use the variable in
addTo(
array(
$customers
)
)->
shows an error, but if I use the string directly the email is sent
addTo(
array(
'email1#email.com' => '', 'email2#email.com' => '', 'email3#email.com' => ''
)
)->
And I don't know why it shows error if the $customers variable has the string that is needed.
Do you know how to use the variable with the emails that I need to send?
You don't build an array by concatenating strings with => in them. To create an element in an associative array, just assign to that array index.
$customers = [];
foreach ($customers as $customer) {
for ($i = 1; $i < 41; $i++) {
if (!empty($customer["email" . $i])) {
$customers[$customer["email" . $i]] = "";
}
}
}
include 'Mailin.php';
$mailin = new Mailin('senders#sender.com', 'key');
$mailin->
addTo($customers)->
...
Also, see Why non-equality check of one variable against many values always returns true? for why you should have used && rather than || when you were skipping empty emails (which I've simplified by using !empty()).
I have the following PHP code:
$special_files = array(
array("Turnip", "Tweed"),
array("Donald", "Trump")
);
I want to be able to get the second value in a nested array by identifying a first. eg: if_exists("Donald") would return "trump".
I've tried to recurse through the array but I'm at a loss on how to select the second value once the first is identified.
Any help would be appreciated
You can use something like this:
$special_files = array(
array("Turnip", "Tweed"),
array("Donald", "Trump")
);
$search_val = "Donald";
$key = array_search($search_val, array_column($special_files,0));
$output = $special_files[$key][1]; //outputs "Trump"
Here is a working sample.
Well, you can try the following:
foreach ($special_files as $special_file) {
$i = 1;
foreach ($special_file as $element) {
if ($i==2) {
echo ("Second value is: " . $element);
break;
}
$i++;
}
}
You can extract the [1] elements and index them by the [0] elements:
$lookup = array_column($special_files, 1, 0);
$result = isset($lookup['Donald']) ?: false;
The $lookup array yields:
Array
(
[Turnip] => Tweed
[Donald] => Trump
)
I have a variable $a='san-serif' and an array Font_list[] now I want only the arrays whose category is 'san-serif' will be filtered. I tried a lot of codes nothing seems working here is my code:-
public function filterFont() {
$a = $_POST['key'];
$url = "https://www.googleapis.com/webfonts/v1/webfonts?key=''";
$result = json_decode(file_get_contents( $url ));
$font_list = "";
foreach ( $result->items as $font )
{
$font_list[] = [
'font_name' => $font->family,
'category' => $font->category,
'variants' => implode(', ', $font->variants),
// subsets
// version
// files
];
}
$filter = filter($font_list);
print_r(array_filter($font_list, $filter));
}
Please help me :-(
What i understood according to that you want something like below:-
<?php
$a='san-serif'; // category you want to search
$font_list=Array('0'=>Array('font_name' => "sans-sherif",'category' => "san-serif"),'1'=>Array('font_name' => "times-new-roman",'category' => "san-serif"),'2'=>Array('font_name' => "sans-sherif",'category' => "roman"));
// your original array seems something like above i mentioned
echo "<pre/>";print_r($font_list); // print original array
$filtered_data = array(); // create new array
foreach($font_list as $key=>$value){ // iterate through original array
if($value['category'] == $a){ // if array category name is equal to serach category name
$filtered_data[$key] = $value; // assign that array to newly created array
}
}
echo "<pre/>";print_r($filtered_data); // print out new array
Output:- https://eval.in/597605
ok below is my code
<?php
// Last 10 Jobs
function last10IT(){
$xml = simplexml_load_file('http://www.cv-library.co.uk/cgi-bin/feed.xml?affid=101899');
$new_array = array();
//$limit = 5;
//$c = 0;
foreach ($xml->jobs->job as $job) {
// if ($limit == $c) {
// break;
// }
$jobref = $job->jobref;
$title = $job->title;
$date = $job->date;
$new_array[$jobref.$date] = array(
'jobref' => $jobref,
'date' => $date,
'title' => $title,
'salary' => $job->salary,
'location' => $job->location,
);
}
}
ksort($new_array);
$showl = 10;
$n = 0;
foreach ($new_array as $date => $listing) {
print $listing['title'] . PHP_EOL;
}
?>
All I want it to do is filter by category & display a max of 10 results
for example
IT
so is there a way I can pass the category value into the function that I want it to filter by
instead of having to replicate for each category
All I get is :
Warning: ksort() expects parameter 1 to be array, null given in
C:\wamp\www\RECRUITMENTFAIR\functions.php on line 28
Please help guys
It something SO simple causing this error but its driving me mad because I just cannot see it
it is relative simple: you try to use the variable $new_array out of scope:
it is defined within your last10IT() function, but the function ends after the first foreach.
you should either return the array and call the function to get the array or move the part with the ksort and the printing into the function, depending on your needs.
Also was having issues by not having the PHP Extension xmlrpc enabled !
what a tool !
Thats why i was getting failed to open half the time
Good Day
I have an array containing data seperated by a comma:
array (
[0]=>Jack,140101d,10
[1]=>Jack,140101a,15
[2]=>Jack,140101n,20
[3]=>Jane,141212d,20
[4]=>Jane,141212a,25
[5]=>Jane,141212n,30
)
There is a lot of data and I would like the data to be set out as:
array(
[Jack]=>
[140101]
=>[d] =>10
=>[a] =>15
=>[n] =>20
)
My code:
foreach ($out as $datavalue) {
$dat = str_getcsv($datavalue,',');
$datevalue = substr($dat[1],2,-1);
$shiftvalue = substr($dat[1],-1);
$totalvalue = $dat[2];
$sval[$shiftvalue] = $totalvalue;
$dval[$datevalue] = $sval;
$opvalue = $dat[0];
$final[$opvalue] = $dval;
}
Now it seems the array is populated even if there is no data from the original string, so my output shows results for Jack on the other dates even though there was no data for him originally. Hope this makes sense. Could anyone point out or suggest a solution please?
As mentioned in the comments, explode is what you need. See this working here.
<?php
$input = array (
0 => 'Jack,140101d,10',
1 => 'Jack,140101a,15',
2 => 'Jack,140101n,20',
3 => 'Jane,141212d,20',
4 => 'Jane,141212a,25',
5 => 'Jane,141212n,30',
);
$result = array();
foreach ($input as $key => $value) {
$valueParts = explode(',',$value); // now valueparts is an array like ('Jack','140101d','10')
$namePart = $valueParts[0];
$idPart = substr($valueParts[1],0,-1); // we need to strip the letter from the id
$charPart = substr($valueParts[1],-1); // and the id from the letter
$nrPart = $valueParts[2]; // you could use intval() to make this an integer rather than a string if you want
// Now we fill the array
if(!array_key_exists($namePart, $result)) {
$result[$namePart] = array();
}
if(!array_key_exists($idPart, $result[$namePart])) {
$result[$namePart][$idPart] = array();
}
if(!array_key_exists($charPart, $result[$namePart][$idPart])) {
$result[$namePart][$idPart][$charPart] = $nrPart;
}
}
var_dump($result);