What does mean undefined offset issue? - php

I can't figure out where is mistake in this code and how to solve this error.
Error reported is:
Notice: Undefined offset: 1 in /Applications/XAMPP/xamppfiles/htdocs/bat/rd-search.php on line 53 that is this string of code:
$final_result[$file_count]['page_title'][] = $page_title[1];
the code is:
$contents = file_get_contents($file);
preg_match("/\<title\>(.*)\<\/title\>/", $contents, $page_title); //getting page title
if (preg_match("#\<body.*\>(.*)\<\/body\>#si", $contents, $body_content)) { //getting content only between <body></body> tags
$clean_content = strip_tags($body_content[0]); //remove html tags
$clean_content = preg_replace('/\s+/', ' ', $clean_content); //remove duplicate whitespaces, carriage returns, tabs, etc
$found = strpos_recursive(mb_strtolower($clean_content, 'UTF-8'), $search_term);
$final_result[$file_count]['page_title'][] = $page_title[1];
$final_result[$file_count]['file_name'][] = preg_replace("/^.{3}/", "\\1", $file);
}
for ($j = 0; $j < count($template_tokens); $j++) {
if (preg_match("/\<meta\s+name=[\'|\"]" . $template_tokens[$j] . "[\'|\"]\s+content=[\'|\"](.*)[\'|\"]\>/", $contents, $res)) {
$final_result[$file_count][$template_tokens[$j]] = $res[1];
}
}

This is because the preg_match returns no matches.
You can resolve this by adding a line below such as the following:
preg_match("/\<title\>(.*)\<\/title\>/", $contents, $page_title); //getting page title
$page_title = empty($page_title) ? [0 => '', 1 => ''] : $page_title;
This will just create an array with 0 and 1 as indexes if preg_match failed to get any matches for a title.
The other alternative is to check the value of $page_title before trying to access it. This approach means that you will have to create a check each time you want to access this variable, whereas the first approach doesn't because the indexes have blank values.
if (!empty($page_title) {
$final_result[$file_count]['page_title'][] = $page_title[1];
}

Related

PHP pred_split Regex expression to return html tags within a sub-string

I have read many threads on this regex question, but none of them seem to work. I am sure it's a result of me really not understanding regex expressions.
Let's say I have the following string:
string='<i>he<a herf="http://www.cnn.com">ll</b>o</i>'
I would like to grab the html tags buried in the substring "hello". I am using the following php function:
$temp = preg_split('/[0-9A-Za-z]+</', $string);
What I am looking for is an array with the following:
a herf="http://www.cnn.com">, and /b>
I can tack on the leading '<'. My results, using the above regex in my preg_split call seem to be including the first '' tag
My full code:
$string = '<i>he<a herf="http://www.cnn.com">ll</b>o</i>';
$temp = preg_split('/[0-9A-Za-z]+</', $string);
echo('<pre>');print_r($temp);echo('</pre>');
$num = count($temp);
$counter = 1;
foreach($temp as $key=>$tag_stem){
if($counter<$num) {
echo('<xmp>');print_r('tag_stem = ' . $tag_stem);echo('</xmp>');
$temp_tag = '<' . $tag_stem;
echo('<xmp>');print_r('temp tag = ' . $temp_tag);echo('</xmp>');
if (empty($temp2)) {
$temp2 = str_replace($temp_tag, '', $string);
} else {
$temp2 = str_replace($temp_tag, '', $temp2);
}
echo('<xmp>');print_r('string = ' . $temp2);echo('</xmp>');
if (strstr($temp_tag, '</')) {
$temp2 = $temp2 . $temp_tag;
} else {
$temp2 = $temp_tag . $temp2;
}
echo('<xmp>');print_r("new string = " . $temp2);echo('</xmp>');
}
$counter++;
}
$temp_array = explode($word, $string);
echo('<xmp>');print_r("final string = " . $temp2);echo('</xmp>');
My results are as follows:
tag_stem = <i>
temp tag = <<i>
string = <i>he<a herf="http://www.cnn.com">ll</b>o</i>
new string = <<i><i>he<a herf="http://www.cnn.com">ll</b>o</i>
tag_stem = a herf="http://www.cnn.com">
temp tag = <a herf="http://www.cnn.com">
string = <<i><i>hell</b>o</i>
new string = <a herf="http://www.cnn.com"><<i><i>hell</b>o</i>
tag_stem = /b>
temp tag = </b>
string = <a herf="http://www.cnn.com"><<i><i>hello</i>
new string = <a herf="http://www.cnn.com"><<i><i>hello</i></b>
final string = <a herf="http://www.cnn.com"><<i><i>hello</i></b>
Not the first iteration. For whatever reason, it's picking up the first "<i>".

Place content in between paragraphs without images

I am using the following code to place some ad code inside my content .
<?php
$content = apply_filters('the_content', $post->post_content);
$content = explode (' ', $content);
$halfway_mark = ceil(count($content) / 2);
$first_half_content = implode(' ', array_slice($content, 0, $halfway_mark));
$second_half_content = implode(' ', array_slice($content, $halfway_mark));
echo $first_half_content.'...';
echo ' YOUR ADS CODE';
echo $second_half_content;
?>
How can i modify this so that the 2 paragraphs (top and bottom) enclosing the ad code should not be the one having images. If the top or bottom paragraph has image then try for next 2 paragraphs.
Example: Correct Implementation on the right.
preg_replace version
This code steps through every paragraph ignoring those that contain image tags. The $pcount variable is incremented for every paragraph found without an image, if an image is encountered however, $pcount is reset to zero. Once $pcount reaches the point where it would hit two, the advert markup is inserted just before that paragraph. This should leave the advert markup between two safe paragraphs. The advert markup variable is then nullified so only one advert is inserted.
The following code is just for set up and could be modified to split the content differently, you could also modify the regular expression that is used — just in case you are using double BRs or something else to delimit your paragraphs.
/// set our advert content
$advert = '<marquee>BUY THIS STUFF!!</marquee>' . "\n\n";
/// calculate mid point
$mpoint = floor(strlen($content) / 2);
/// modify back to the start of a paragraph
$mpoint = strripos($content, '<p', -$mpoint);
/// split html so we only work on second half
$first = substr($content, 0, $mpoint);
$second = substr($content, $mpoint);
$pcount = 0;
$regexp = '/<p>.+?<\/p>/si';
The rest is the bulk of the code that runs the replacement. This could be modified to insert more than one advert, or to support more involved image checking.
$content = $first . preg_replace_callback($regexp, function($matches){
global $pcount, $advert;
if ( !$advert ) {
$return = $matches[0];
}
else if ( stripos($matches[0], '<img ') !== FALSE ) {
$return = $matches[0];
$pcount = 0;
}
else if ( $pcount === 1 ) {
$return = $advert . $matches[0];
$advert = '';
}
else {
$return = $matches[0];
$pcount++;
}
return $return;
}, $second);
After this code has been executed the $content variable will contain the enhanced HTML.
PHP versions prior to 5.3
As your chosen testing area does not support PHP 5.3, and so does not support anonymous functions, you need to use a slightly modified and less succinct version; that makes use of a named function instead.
Also, in order to support content that may not actually leave space for the advert in it's second half I have modified the $mpoint so that it is calculated to be 80% from the end. This will have the effect of including more in the $second part — but will also mean your adverts will be generally placed higher up in the mark-up. This code has not had any fallback implemented into it, because your question does not mention what should happen in the event of failure.
$advert = '<marquee>BUY THIS STUFF!!</marquee>' . "\n\n";
$mpoint = floor(strlen($content) * 0.8);
$mpoint = strripos($content, '<p', -$mpoint);
$first = substr($content, 0, $mpoint);
$second = substr($content, $mpoint);
$pcount = 0;
$regexp = '/<p>.+?<\/p>/si';
function replacement_callback($matches){
global $pcount, $advert;
if ( !$advert ) {
$return = $matches[0];
}
else if ( stripos($matches[0], '<img ') !== FALSE ) {
$return = $matches[0];
$pcount = 0;
}
else if ( $pcount === 1 ) {
$return = $advert . $matches[0];
$advert = '';
}
else {
$return = $matches[0];
$pcount++;
}
return $return;
}
echo $first . preg_replace_callback($regexp, 'replacement_callback', $second);
You could try this:
<?php
$ad_code = 'SOME SCRIPT HERE';
// Your code.
$content = apply_filters('the_content', $post->post_content);
// Split the content at the <p> tags.
$content = explode ('<p>', $content);
// Find the mid of the article.
$content_length = count($content);
$content_mid = floor($content_length / 2);
// Save no image p's index.
$last_no_image_p_index = NULL;
// Loop beginning from the mid of the article to search for images.
for ($i = $content_mid; $i < $content_length; $i++) {
// If we do not find an image, let it go down.
if (stripos($content[$i], '<img') === FALSE) {
// In case we already have a last no image p, we check
// if it was the one right before this one, so we have
// two p tags with no images in there.
if ($last_no_image_p_index === ($i - 1)) {
// We break here.
break;
}
else {
$last_no_image_p_index = $i;
}
}
}
// If no none image p tag was found, we use the last one.
if (is_null($last_no_image_p_index)) {
$last_no_image_p_index = ($content_length - 1);
}
// Add ad code here with trailing <p>, so the implode later will work correctly.
$content = array_slice($content, $last_no_image_p_index, 0, $ad_code . '</p>');
$content = implode('<p>', $content);
?>
It will try to find a place for the ad from the mid of your article and if none is found the ad is put to the end.
Regards
func0der
I think this will work:
First explode the paragraphs, then you have to loop it and check if you find img inside them.
If you find it inside, you try the next.
Think of this as psuedo-code, since it's not tested. You will have to make a loop too, comments in the code :) Sorry if it contains bugs, it's written in Notepad.
<?php
$i = 0; // counter
$arrBoolImg = array(); // array for the paragraph booleans
$content = apply_filters('the_content', $post->post_content);
$contents = str_replace ('<p>', '<explode><p>', $content); // here we add a custom tag, so we can explode
$contents = explode ('<explode>', $contents); // then explode it, so we can iterate the paragraphs
// fill array with boolean array returned
$arrBoolImg = hasImages($contents);
$halfway_mark = ceil(count($contents) / 2);
/*
TODO (by you):
---
When you have $arrBoolImg filled, you can itarate through it.
You then simply loop from the middle of the array $contents (plural), that is exploded from above.
The startingpoing for your loop is the middle, the upper bounds is the +2 or what ever :-)
Then you simply insert your magic.. And then glue it back together, as you did before.
I think this will work. even though the code may have some bugs, since I wrote it in Notepad.
*/
function hasImages($contents) {
/*
This function loops through the $contents array and checks if they have images in them
The return value, is an array with boolean values, so one can iterate through it.
*/
$arrRet = array(); // array for the paragraph booleans
if (count($content)>=1) {
foreach ($contents as $v) { // iterate the content
if (strpos($v, '<img') === false) { // did not find img
$arrRet[$i] = false;
}
else { // found img
$arrRet[$i] = true;
}
$i++;
} // end for each loop
return $arrRet;
} // end if count
} // end hasImages func
?>
[This is just an idea, I don't have enough reputation to comment...]
After calling #Olavxxx's method and filling your boolean array you could just loop through that array in an alternating manner starting in the middle: Let's assume your array is 8 entries long. Calculating the middle using your method you get 4. So you check the combination of values 4 + 3, if that doesn't work, you check 4 + 5, after that 3 + 2, ...
So your loop looks somewhat like
$middle = ceil(count($content) / 2);
$i = 1;
while ($i <= $middle) {
$j = $middle + (-1) ^ $i * $i;
$k = $j + 1;
if (!$hasImagesArray[$j] && !$hasImagesArray[$k])
break; // position found
$i++;
}
It's up to you to implement further constraints to make sure the add is not shown to far up or down in the article...
Please note that you need to take care of special cases like too short arrays too in order to prevent IndexOutOfBounds-Exceptions.

Strict standards: Only variables should be passed by reference in get_id.php on line 27

i get the following notice in php :
Strict standards: Only variables should be passed by reference in get_id.php on line 27
the code is :
<?php
function truncate($text, $length, $suffix ='', $isHTML = true) {
$i = 0;
$simpleTags=array('br'=>true,'hr'=>true,'input'=>true,'image'=>true,'link'=>true,'meta'=>true);
$tags = array();
if($isHTML){
preg_match_all('/<[^>]+>([^<]*)/', $text, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
foreach($m as $o){
if($o[0][1] - $i >= $length)
break;
$t = substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1);
// test if the tag is unpaired, then we mustn't save them
if($t[0] != '/' && (!isset($simpleTags[$t])))
$tags[] = $t;
elseif(end($tags) == substr($t, 1))
array_pop($tags);
$i += $o[1][1] - $o[0][1];
}
}
// output without closing tags
$output = substr($text, 0, $length = min(strlen($text), $length + $i));
// closing tags
$output2 = (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '');
// Find last space or HTML tag (solving problem with last space in HTML tag eg. <span class="new">)
$pos = (int)end(end(preg_split('/<.*>| /', $output, -1, PREG_SPLIT_OFFSET_CAPTURE)));
// Append closing tags to output
$output.=$output2;
// Get everything until last space
$one = substr($output, 0, $pos);
// Get the rest
$two = substr($output, $pos, (strlen($output) - $pos));
// Extract all tags from the last bit
preg_match_all('/<(.*?)>/s', $two, $tags);
// Add suffix if needed
if (strlen($text) > $length) { $one .= $suffix; }
// Re-attach tags
$output = $one . implode($tags[0]);
//added to remove unnecessary closure
$output = str_replace('</!-->','',$output);
return $output;
}
?>
how do i remove this notice.. Please Help. I am using WampServer 2.4.
This notice won't show in older version and in linux hosting. but the new wampserver shows this notice.
Thanks in advance...
It’s end() that expects a reference because it changes the internal array value pointer
there are a lot of similiar questions here.
end expects its parameter to be a passed by reference, and only variables can be passed by reference (not the return value of another function, like preg_split in your case)
easiest solution should be to split the calls to separate lines, e.g.
$splitted = preg_split('/<.*>| /', $output, -1, PREG_SPLIT_OFFSET_CAPTURE);
$last_item = end($splitted);
$very_last_item = end($last_item);
or, if you have the time and stuff, rework this function so you don't have to end() calls
(sorry, I kinda can't get into what you want to accomplish here)

simpleHTMLdom - Using multiple URLs from an array

In my UI there is a text area that has a list of URLs separated by new line.
I am getting this into a string and exploding it using newline character. I am getting this error:
Fatal error: Call to a member function find() on a non-object in C:\xampp\htdocs\apps\lt\track-them.php on line 34
$myurl = "http://mydomain.com/testpage1.html";
$str = "http://www.domain.com/page1.html
http://www.homepage.com/page2.html
http://www.internet.com/index.html";
$backlinkarr = explode("\n", $str);
echo '<table border=1>';
foreach ($backlinkarr as $backlinkitem) {
$backlinkitem = trim($backlinkitem);
$LinkCount = 0;
$html = file_get_html($backlinkitem);
foreach ($html->find('a') as $link) {
if ($link->href == $myurl) {
$LinkCount += $LinkCount + 1;
}
}
echo $backlinkitem . ' (' . $linkCount . ')';
}
on windows new line is \r\n
also, your error suggests that $html is not an object, meaning previous line fails. so, debug there with var_dump($backlinkitem) if it is correct and also you can use file_exists to verify if that file actually exists.

Help with string parsing

I have a huge library file containing a word and it's synonyms, this is some words and their synonyms in the format of my library:
aantarrão|1
igrejeiro|igrejeiro|aantarrão|beato
aãsolar|1
desolar|desolar|aãsolar|afligir|arrasar|arruinar|consternar|despovoar|devastar|magoar
aba|11
amparo|amparo|aba|abrigo|achego|acostamento|adminículo|agasalho|ajuda|anteparo|apadrinhamento|apoio|arrimo|asilo|assistência|auxíjlio|auxílio|baluarte|bordão|broquel|coluna|conchego|defesa|égide|encosto|escora|esteio|favor|fulcro|muro|patrocínio|proteção|proteçâo|resguardo|socorro|sustentáculo|tutela|tutoria
apoio|apoio|aba|adesão|adminículo|amparo|aprovação|arrimo|assentimento|base|bordão|coluna|conchego|descanso|eixo|encosto|escora|espeque|fé|fulcro|proteçâo|proteção|refúgio|socorro|sustentáculo
beira|beira|aba|beirada|borda|bordo|cairel|encosta|extremidade|falda|iminência|margem|orla|ourela|proximidade|rai|riba|sopé|vertente
beirada|beirada|aba|beira|encosta|falda|margem|sopé|vertente
encosta|encosta|aba|beira|beirada|clivo|falda|lomba|sopé|subida|vertente
falda|falda|aba|beira|beirada|encosta|fralda|sopé|vertente
fralda|fralda|aba|falda|raiss|raiz|sopé
prestígio|prestígio|aba|auréola|autoridade|domínio|força|halo|importância|influência|preponderância|valia|valimento|valor
proteção|proteção|aba|abrigo|agasalho|ajuda|amparo|apoio|arrimo|asilo|auspiciar|auxílio|bafejo|capa|custódia|defesa|égide|escora|fautoria|favor|fomento|garantia|paládio|patrocínio|pistolão|quartel|refúgio|socorro|tutela|tutoria
sopé|sopé|aba|base|beira|beirada|encosta|falda|fralda|raiz|vertente
vertente|vertente|aba|beira|beirada|declive|encosta|falda|sopé
see aantarrão is a word and below it are the synonyms, I can't think of a way to get the word and the synonyms on an associative array, this is what I'm trying to do:
<?
$file = file('library.txt');
$array_sinonimos = array();
foreach($file as $k)
{
$explode = explode($k, "|");
if(is_int($explode[1]))
{
$word = $explode[0];
}
}
?>
nothing, lol, what can I do here ? loop lines until I find an empty line then try to get a new word with the explode ?, help !
Here's some code I cooked up that seems to work.
See the code in action here: http://codepad.org/TVpYgW91
See the code here
UPDATED to read line by line
<?php
$filepointer = fopen("library.txt", "rb");
$words = array();
while(!feof($filepointer)) {
$line = trim(fgets($filepointer));
$content = explode("|", $line);
if (count($content) == 0)
continue;
if (is_numeric(end($content))) {
$word = reset($content);
continue;
}
if (isset($words[$word]))
$words[$word] = array_merge($words[$word], $content);
else
$words[$word] = $content;
}
print_r($words);
So what's the strategy?
fix up the line endings
run through the file line by line
ignore empty lines (count($content))
split the line up on the pipes, if the line has a numerical value for the last value, then this becomes our word
we only get to the last step if none of the other traps got touched, because of the continue statements, so if it is then just split up the words by the pipe and add them to or create the array element.
Try this. I can't remember if array_merge() will work with a null, but the basic idea is that $word is the $key to the assoc array.
<?
$file = file('library.txt');
$array_sinonimos = array();
foreach($file as $k)
{
$explode = explode($k, "|");
if(is_int($explode[1]))
{
$word = $explode[0];
}
else if(!empty($explode))
{
$array_sinonimos[$word] = array_merge($synonyms[$word], $explode);
}
}
?>

Categories