what i try to do ?
i try to make code to found if there duplicate data into database or not ,that data it text Content written by users .
so i make sql query to get result of row pagetext from database and clean it from any Signs And coordinate (font color, font size, and font type) and image links or any links , and same i do it for Variable $post['message'] that get Content of text area ..
than i check if Content of $post['message'] is same Content of pagetext or not !
Full code :
// FUNCTION TO CLEAN TEXT
function stripBBCode($text_to_search)
{
$pattern = '|[[\/\!]*?[^\[\]]*?]|si';
$replace = '';
return preg_replace($pattern, $replace, $text_to_search);
}
// MYSQL QUERY
$ckeck = $db->query_read(" SELECT pagetext FROM " . TABLE_PREFIX . " post ");
$ckeck_num = mysql_num_rows($ckeck);
while ($ckeckpagetext = $db->fetch_array($ckeck))
{
// RESULT FROM QUERY - here i try to make ARRAY BY [square brackets]
$pagetext[] = stripBBCode($ckeckpagetext['pagetext']);
$pagetext[] = preg_replace('/[\s]+/mu','', $pagetext);
$pagetext[] = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|$!:,.;]*[A-Z0-9+&##\/%=~_|$]/i', '', $pagetext);
// Variable
$message = stripBBCode($post['message']);
$message = preg_replace('/[\s]+/mu','',$message );
$message = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|$!:,.;]*[A-Z0-9+&##\/%=~_|$]/i', '', $message);
// LOOP
for($x=0; $x<$ckeck_num; $x++)
{
// CHECK IF THERE duplicate TEXT OR NOT
if ($message == $pagetext[$x])
{
$ckeck_duplicate = 1;
}else{
$ckeck_duplicate = 2;
}
}
}
My problem ?
my code Almost correct , but my problem in those lines [square brackets] . when i try great array for my result
// RESULT FROM QUERY
$pagetext[] = stripBBCode($ckeckpagetext['pagetext']);
$pagetext[] = preg_replace('/[\s]+/mu','', $pagetext);
$pagetext[] = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|$!:,.;]*[A-Z0-9+&##\/%=~_|$]/i', '', $pagetext);
if i used only first line without used preg_replace the code works good .
when i use [square brackets] for a
$pagetext[] = stripBBCode($ckeckpagetext['pagetext']);
$pagetext = array();
while ($ckeckpagetext = $db->fetch_array($ckeck))
{
$tmpCheckPageText = stripBBCode($ckeckpagetext['pagetext']);
$tmpCheckPageText = preg_replace('/[\s]+/mu','', $tmpCheckPageText);
$tmpCheckPageText = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|$!:,.;]*[A-Z0-9+&##\/%=~_|$]/i', '', $tmpCheckPageText);
$pagetext[] = $tmpCheckPageText;
}
And array is:
$pagetext = array( 'Plain text1', 'Plain text2' );
Try Your function stripBBCode and preg_replace on variable for example $tmpCheckPageText and after this put into array $pagetext
Code:
$tmpCheckPageText = stripBBCode($ckeckpagetext['pagetext']);
$tmpCheckPageText = preg_replace('/[\s]+/mu','', $tmpCheckPageText);
$tmpCheckPageText = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|$!:,.;]*[A-Z0-9+&##\/%=~_|$]/i', '', $tmpCheckPageText);
$pagetext[] = $tmpCheckPageText;
UPDATE:
Create array of pagetext from db using sql statement and purge it in while loop. For example you got this:
$pagetext = array( 'Purge Text 1', 'Purge Text 2', 'Purge text 3' );
After this, purge Your post field $message. Next check with in_array function.
echo in_array( $message, $pagetext ) ? 1 : 2;
Related
I want to get all names of my products and push them into an array. If I use the SQL Query (SELECT name FROM wp_all_import_xml GROUP BY name) in phpmyadmin I get this:
But if I use the query in my script I get this array:
array (
0 => NULL,
1 => NULL,
2 => NULL,
3 => NULL,
4 => NULL,
...)
So, the query must return null. But why? - Everything is correct!
I do not get any connection error or query error... Where is my error?
I already tried to decode the strings and I also tried $myTitles[] = $tmp. But it also doesn't work... Any ideas?
And because everything in the $myTitles array is null, the script always enters the if(!(in_array($title, $myTitles) condition and I cannot execute what I actual want... I can only execute this, if the array and the condition is really true!
<?php
if ( ! defined('ABSPATH') ) {
// Set up WordPress environment
require_once( dirname( __FILE__ ) . '/wp-load.php' );
}
unlink('deleteOldProducts.txt');
$myfile = fopen('deleteOldProducts.txt', "w");
$database_gk = new mysqli("localhost", "censored", "censored", "censored");
$database_jt = new mysqli("localhost", "censored", "censored", "censored");
$myTitles = array();
$string = "";
if($database_jt->connect_errno){
$string .= "+++Couldn't connect to database_jt!+++\n\n");
}
if($database_gk->connect_errno){
$string .= "+++Couldn't connect to database_gk!+++\n\n");
}
$values_jt = $database_jt->query("SELECT name FROM `wp_all_import_xml` GROUP BY name");
$string .= $database_jt->error . "\n";
while($rowj = $values_jt->fetch_assoc()){
$tmp = utf8_encode($row["name"]);
array_push($myTitles, $tmp);
$tmp = null;
}
$values_gk = $database_gk->query("SELECT `post_title` FROM `fWR6qIN_posts` where post_type = 'product' AND post_status = 'publish' GROUP BY `post_title`");
$string .= $database_gk->error . "\n";
$string .= "+++ Start of Check +++\n\nMein Array:\n" . var_export($myTitles, true) . "\n\n";
$i = 1;
while($row = $values_gk->fetch_assoc()){
$title = utf8_decode($row["post_title"]);
if(!(in_array($title, $myTitles))){
$string .= $i . ":\t" . $title . "\n";
$i = $i + 1;
}
$title = null;
}
$string .= "\n +++ End of Check! +++";
fwrite($myfile, $string);
fclose($myfile);
?>
Greetings and Thank You!
Your while isn't correct. On while you are using $rowj but on array_push you are using $row. You can try the following code instead:
while($row = $values_jt->fetch_assoc()) {
$myTitles[] = utf8_encode($row["name"]);
}
Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
http://php.net/manual/en/function.array-push.php
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>".
I am displaying search results on site where users can search for specific keyword, words.
On results page I am trying to Highlight the searched words , in the result.
So user can get idea which words matched where.
e.g.
if user searches for : mango
the resulting item original : This Post contains Mango.
the resulting output I want of highlighted item : This Post contains <strong>Mango</strong>
I am using it like this.
<?php
//highlight all words
function highlight_words( $title, $searched_words_array) {
// loop through searched_words_array
foreach( $searched_words_array as $searched_word ) {
$title = highlight_word( $title, $searched_word); // highlight word
}
return $title; // return highlighted data
}
//highlight single word with color
function highlight_word( $title, $searched_word) {
$replace = '<strong>' . $searched_word . '</strong>'; // create replacement
$title = str_ireplace( $searched_word, $replace, $title ); // replace content
return $title; // return highlighted data
}
I am getting searched words from Sphinx Search Engine , the issue is Sphinx returns entered/macthed words in lowercase.
So by using above code , my
results becomes : This Post contains <strong>mango</strong>
*notice the m from mango got lowercase.
So my question is how can I Highlight word i.e. wrap <strong> & </strong> around the words matching the Searched words ?
without loosing its textcase ?
*ppl. its not same questions as how to highlight search results , I am asking my keywords array is in lowercase and using above method the original word gets replaced by lowercase word.
so how can I stop that ?
the other question link will face this too , because the searched keywords are in lowercase. and using str_ireplace it will match it and replace it with lowercase word.
update :
i have combined various code snippets to get what i was expecting code to do.,
for now its working great.
function strong_words( $title, $searched_words_array) {
//for all words in array
foreach ($searched_words_array as $word){
$lastPos = 0;
$positions = array();
//find all positions of word
while (($lastPos = stripos($title, $word, $lastPos))!== false) {
$positions[] = $lastPos;
$lastPos = $lastPos + strlen($word);
}
//reverse sort numeric array
rsort($positions);
// highlight all occurances
foreach ($positions as $pos) {
$title = strong_word($title , $word, $pos);
}
}
//apply strong html code to occurances
$title = str_replace('#####','</strong>',$title);
$title = str_replace('*****','<strong>',$title);
return $title; // return highlighted data
}
function strong_word($title , $word, $pos){
//ugly hack to not use <strong> , </strong> here directly, as it can get replaced if searched word contains charcters from strong
$title = substr_replace($title, '#####', $pos+strlen($word) , 0) ;
$title = substr_replace($title, '*****', $pos , 0) ;
return $title;
}
$title = 'This is Great Mango00lk mango';
$words = array('man','a' , 'go','is','g', 'strong') ;
echo strong_words($title,$words);
Regex solution:
function highlight_word( $title, $searched_word) {
return preg_replace('#('.$searched_word.')#i','<strong>\1<strong>',$title) ;
}
Just be wary of special characters that may be interpreted as meta characters in $searched_word
Here's a code snippet I wrote a while back that's working to do exactly what you want:
if(stripos($result->question, $word) !== FALSE){
$word_to_highlight = substr($result->question, stripos($result->question, $word), strlen($word));
$result->question = str_replace($word_to_highlight, '<span class="search-term">'.$word_to_highlight.'</span>', $result->question);
}
//will find all occurances of all words and make them strong in html
function strong_words( $title, $searched_words_array) {
//for all words in array
foreach ($searched_words_array as $word){
$lastPos = 0;
$positions = array();
//find all positions of word
while (($lastPos = stripos($title, $word, $lastPos))!== false) {
$positions[] = $lastPos;
$lastPos = $lastPos + strlen($word);
}
//reverse sort numeric array
rsort($positions);
// highlight all occurances
foreach ($positions as $pos) {
$title = strong_word($title , $word, $pos);
}
}
//apply strong html code to occurances
$title = str_replace('#####','</strong>',$title);
$title = str_replace('*****','<strong>',$title);
return $title; // return highlighted data
}
function strong_word($title , $word, $pos){
//ugly hack to not use <strong> , </strong> here directly, as it can get replaced if searched word contains charcters from strong
$title = substr_replace($title, '#####', $pos+strlen($word) , 0) ;
$title = substr_replace($title, '*****', $pos , 0) ;
return $title;
}
$title = 'This is Great Mango00lk mango';
$word = array('man','a' , 'go','is','g', 'strong') ;
echo strong_words($title,$word);
This code will find all occurrences of all words and make them strong in html while keeping original text case.
function highlight_word( $content, $word, $color ) {
$replace = '<span style="background-color: ' . $color . ';">' . $word . '</span>'; // create replacement
$content = str_replace( $word, $replace, $content ); // replace content
return $content; // return highlighted data
}
function highlight_words( $content, $words, $colors ) {
$color_index = 0; // index of color (assuming it's an array)
// loop through words
foreach( $words as $word ) {
$content = highlight_word( $content, $word, $colors[$color_index] ); // highlight word
$color_index = ( $color_index + 1 ) % count( $colors ); // get next color index
}
return $content; // return highlighted data
}
// words to find
$words = array(
'normal',
'text'
);
// colors to use
$colors = array(
'#88ccff',
'#cc88ff'
);
// faking your results_text
$results_text = array(
array(
'ab' => 'AB #1',
'cd' => 'Some normal text with normal words isn\'t abnormal at all'
), array(
'ab' => 'AB #2',
'cd' => 'This is another text containing very normal content'
)
);
// loop through results (assuming $output1 is true)
foreach( $results_text as $result ) {
$result['cd'] = highlight_words( $result['cd'], $words, $colors );
echo '<fieldset><p>ab: ' . $result['ab'] . '<br />cd: ' . $result['cd'] . '</p></fieldset>';
}
Original link check here
I have a dynamic content which have few tags starting with "https://" and "http://". I need to append title for each anchor tag starting with "https". code which I am using not able to append dynamic title. for ex:
$str = 'some text
some other text ';
The output string would be:
$str = 'some text
some text '
The code which I am using is:
$xml = '<foo>'. $content.'</foo>';
$doc = new DOMDocument;
$doc->loadXml($xml);
foreach ($doc->getElementsByTagName('a') as $anchor) {
if ($anchor->hasAttribute('title')) {
$anchor->removeAttribute('title');
}
}
$newcontent = $doc->saveHTML();
$pattern = '/(href=("|\')https)(:\\/\\/.*?("|\'))/';
$subject = $newcontent;
preg_match_all ($pattern, $subject, $matches);
for($i=0; $i< count($matches); $i++){
$complete_url = $matches[0][$i];
$get_prog_name = explode("//",$complete_url);
if (strpos($get_prog_name[1],'www.') !== false) {
$get_prog_name = explode("www.",$complete_url);
}
$prog_acro = explode(".", $get_prog_name[1]);
if($prog_acro[0] == "ccc") {
$progName = "Dynamic Title 1";
}
if($prog_acro[0] == "cec") {
$progName = "Dynamic Title 2";
}
$replacement[] = 'class="tooltip" title=Requires CEB '.$progName.'membership login $1$3';
} // end foreach loop
$newstr = preg_replace($pattern, $replacement[0], $subject, -1 );
I want to replace title dynamically here. the problem is that when put preg_replace in the loop it prints whole content many time as it contain anhor tags.
I want to retrieve an HTML element in a page.
<h2 id="resultCount" class="resultCount">
<span>
Showing 1 - 12 of 40,923 Results
</span>
</h2>
I have to get the total number of results for the test in my php.
For now, I get all that is between the h2 tags and I explode the first time with space.
Then I explode again with the comma to concatenate able to convert numbers results in European format. Once everything's done, I test my number results.
define("MAX_RESULT_ALL_PAGES", 1200);
$queryUrl = AMAZON_TOTAL_BOOKS_COUNT.$searchMonthUrlParam.$searchYearUrlParam.$searchTypeUrlParam.urlencode($keyword)."&page=".$pageNum;
$htmlResultCountPage = file_get_html($queryUrl);
$htmlResultCount = $htmlResultCountPage->find("h2[id=resultCount]");
$resultCountArray = explode(" ", $htmlResultCount[0]);
$explodeCount = explode(',', $resultCountArray[5]);
$europeFormatCount = '';
foreach ($explodeCount as $val) {
$europeFormatCount .= $val;
}
if ($europeFormatCount > MAX_RESULT_ALL_PAGES) {*/
$queryUrl = AMAZON_SEARCH_URL.$searchMonthUrlParam.$searchYearUrlParam.$searchTypeUrlParam.urlencode($keyword)."&page=".$pageNum;
}
At the moment the total number of results is not well recovered and the condition does not happen even when it should.
Someone would have a solution to this problem or any other way?
I would simply fetch the page as a string (not html) and use a regular expression to get the total number of results. The code would look something like this:
define('MAX_RESULT_ALL_PAGES', 1200);
$queryUrl = AMAZON_TOTAL_BOOKS_COUNT . $searchMonthUrlParam . $searchYearUrlParam . $searchTypeUrlParam . urlencode($keyword) . '&page=' . $pageNum;
$queryResult = file_get_contents($queryUrl);
if (preg_match('/of\s+([0-9,]+)\s+Results/', $queryResult, $matches)) {
$totalResults = (int) str_replace(',', '', $matches[1]);
} else {
throw new \RuntimeException('Total number of results not found');
}
if ($totalResults > MAX_RESULT_ALL_PAGES) {
$queryUrl = AMAZON_SEARCH_URL . $searchMonthUrlParam . $searchYearUrlParam . $searchTypeUrlParam . urlencode($keyword) . '&page=' . $pageNum;
// ...
}
A regex would do it:
...
preg_match("/of ([0-9,]+) Results/", $htmlResultCount[0], $matches);
$europeFormatCount = intval(str_replace(",", "", $matches[1]));
...
Please try this code.
define("MAX_RESULT_ALL_PAGES", 1200);
// new dom object
$dom = new DOMDocument();
// HTML string
$queryUrl = AMAZON_TOTAL_BOOKS_COUNT.$searchMonthUrlParam.$searchYearUrlParam.$searchTypeUrlParam.urlencode($keyword)."&page=".$pageNum;
$html_string = file_get_contents($queryUrl);
//load the html
$html = $dom->loadHTML($html_string);
//discard white space
$dom->preserveWhiteSpace = TRUE;
//Get all h2 tags
$nodes = $dom->getElementsByTagName('h2');
// Store total result count
$totalCount = 0;
// loop over the all h2 tags and print result
foreach ($nodes as $node) {
if ($node->hasAttributes()) {
foreach ($node->attributes as $attribute) {
if ($attribute->name === 'class' && $attribute->value == 'resultCount') {
$inner_html = str_replace(',', '', trim($node->nodeValue));
$inner_html_array = explode(' ', $inner_html);
// Print result to the terminal
$totalCount += $inner_html_array[5];
}
}
}
}
// If result count grater than 1200, do this
if ($totalCount > MAX_RESULT_ALL_PAGES) {
$queryUrl = AMAZON_SEARCH_URL.$searchMonthUrlParam.$searchYearUrlParam.$searchTypeUrlParam.urlencode($keyword)."&page=".$pageNum;
}
Give this a try:
$match =array();
preg_match('/(?<=of\s)(?:\d{1,3}+(?:,\d{3})*)(?=\sResults)/', $htmlResultCount, $match);
$europeFormatCount = str_replace(',','',$match[0]);
The RegEx reads the number between "of " and " Results", it matches numbers with ',' seperator.