I'm here, because I need some help about a searching form display in php.
I've this PHP file (Is a templateMonster plugin rd-search.php) that searches some words into website that users search and types. There is a problem that I can't figure out, because words are searched in html files but not in php files. Can you tell me if it's possible make php files allowable filetypes to search in? I've tried also to add this code
$search_in = array('php','html', 'htm');
but it doesn't work. Why?
To view full code click here:enter link description here
<?php
if (!isset($_GET['s'])) {
die('You must define a search term!');
}
$highlight = true; //highlight results or not
$search_in = array('html', 'htm'); //allowable filetypes to search in
$search_dir = '..'; //starting directory
$recursive = true; //should it search recursively or not
define('SIDE_CHARS', 15);
$file_count = 0;
$search_term = mb_strtolower($_GET['s'], 'UTF-8');
if ($search_term == "?s=") {
$search_term = "";
}
?>
Here below there is a link that explains what I would like to do:
maybe I've found the solution. The problem was in the html code, I solved the issue simply adding aa specific class to a search form.
Thank to all. My best regards to all
Related
My goal is to scrape search results with PHP Simple HTML DOM Parser
which is working fine for me. But after every one or two days, Google changes their HTML structure and my code stop working.
Here's my code that was working before:
include("simple_html_dom.php");
$data = file_get_contents('https://www.google.com/search?q=stackoverflow');
$html = str_get_html($data);
$i=0;
$linkObjs = $html->find('h3[class=r] a');
foreach ($linkObjs as $linkObj) {
$i++;
$url = trim($linkObj->href);
$trim = substr($url, 0, 7);
if ($trim=="/url?q=") {
$url = substr($url, 7);
}
$trim_2 = stripos($url, '&sa=U');
if ($trim_2 != false) {
$url = substr($url, 0, $trim_2);
}
echo "$i:".$url.'<br>';
}
They usually change class names and tag name along with HTML links structure
I had the same problem. Try
$linkObjs = $html->find('div[class=jfp3ef] a');
and it will work again.
I had a similar experience. When I search Google from the ordinary user interface, the URLs of the "hit" pages are still showing up in an A tag (of course) after a div class 'r'. But when I run my scraping program with the exact same search terms and parameters, the 'r' changes to 'kCrYT'. I changed that in my code and got the program working again. (Yay!)
But I suspect the class will change regularly when Google detects that someone is submitting the search programmatically. So this might not be a permanent solution.
Maybe I could add a little extra code that determines what class name is currently being used for this, so that my program could automatically adapt to these changes.
I'm looking for php code or function which can help me to search and find first matching URL, based on specific pattern, from a wordpress post and echo the same url in the same post where it's necessary instead of modifying parent url.
Example Case:
URL:https://example.com/education_system_comparison.html
when I open this page, afer title of the post, there is description of this post, here I want to search from description to find matching url that must starts from my desired domain name like https://some-url-is-here/3978732978937298.html and ends with .html
If anyone here can help me to create a function or use any filters those can work with wordpress or php, it would be so helping and must appreciated. Thank you so much.
I found the solution what I was looking for and it's working perfectly fine on single post or on custom page.
Here is function, I've added in functions.php of my WordPress theme;
function getBetween($content,$start,$end){
$r = explode($start, $content);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return ''; }
so I can call above function by using following code and it work 100% fine, example code:
<?php
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$start = ' https://some-url-is-here/';
$end = '"';
$output = getBetween($content,$start,$end);
echo $start.$output; ?>
It gives output like below based on first matching url and stop immediately
https://some-url-is-here/3978732978937298.html
On the other hand, once I applied the same code throughout my website so I can get every matching url from each and every published post to use it where necessary on the same post. As I save changes, my server got halted coz there were hundreds of httpd requests started processing and server went down immediately.
I'm not sure what exactly is going wrong here, If anyone can guide me and help me what exactly wrong here so it'll be much appreciated and any suggestion or fix, Thanks.
URL : http://www.sayuri.co.jp/used-cars
Example : http://www.sayuri.co.jp/used-cars/B37753-Toyota-Wish-japanese-used-cars
Hey guys , need some help with one of my personal projects , I've already wrote the code to fetch data from each single car url (example) and post on my site
Now i need to go through the main url : sayuri.co.jp/used-cars , and :
1) Make an array / list / nodes of all the urls for all the single cars in it , then run my internal code for each one to fetch data , then move on to the next one
I already have the code to save each url into a log file when completed (don't think it will be necessary if it goes link by link without starting from the top but will ensure no repetition.
2) When all links are done for the page , it should move to the next page and do the same thing until the end ( there are 5-6 pages max )
I've been stuck on this part since last night and would really appreciate any help . Thanks
My code to get data from the main url :
$content = file_get_contents('http://www.sayuri.co.jp/used-cars/');
// echo $content;
and
$dom = new DOMDocument;
$dom->loadHTML($content);
//echo $dom;
I'm guessing you already know this since you say you've gotten data from the car entries themselves, but a good point to start is by dissecting the page's DOM and seeing if there are any elements you can use to jump around quickly. Most browsers have page inspection tools to help with this.
In this case, <div id="content"> serves nicely. You'll note it contains a collection of tables with the required links and a <div> that contains the text telling us how many pages there are.
Disclaimer, but it's been years since I've done PHP and I have not tested this, so it is probably neither correct or optimal, but it should get you started. You'll need to tie the functions together (what's the fun in me doing it?) to achieve what you want, but these should grab the data required.
You'll be working with the DOM on each page, so a convenience to grab the DOMDocument:
function get_page_document($index) {
$content = file_get_contents("http://www.sayuri.co.jp/used-cars/page:{$index}");
$document = new DOMDocument;
$document->loadHTML($content);
return $document;
}
You need to know how many pages there are in total in order to iterate over them, so grab it:
function get_page_count($document) {
$content = $document->getElementById('content');
$count_div = $content->childNodes->item($content->childNodes->length - 4);
$count_text = $count_div->firstChild->textContent;
if (preg_match('/Page \d+ of (\d+)/', $count_text, $matches) === 1) {
return $matches[1];
}
return -1;
}
It's a bit ugly, but the links are available inside each <table> in the contents container. Rip 'em out and push them in an array. If you use the link itself as the key, there is no concern for duplicates as they'll just rewrite over the same key-value.
function get_page_links($document) {
$content = $document->getElementById('content');
$tables = $content->getElementsByTagName('table');
$links = array();
foreach ($tables as $table) {
if ($table->getAttribute('class') === 'itemlist-table') {
// table > tbody > tr > td > a
$link = $table->firstChild->firstChild->firstChild->firstChild->getAttribute('href');
// No duplicates because they just overwrite the same entry.
$links[$link] = "http://www.sayuri.co.jp{$link}";
}
}
return $links;
}
Perhaps also obvious, but these will break if this site changes their formatting. You'd be better off asking if they have a REST API or some such available for long term use, though I'm guessing you don't care as much if it's just a personal project for tinkering.
Hope it helps prod you in the right direction.
I came across this simple pear tutorial over here: http://www.codediesel.com/php/search-replace-in-files-using-php/
include 'File/SearchReplace.php' ;
$files_to_search = array("fruits.txt") ;
$search_string = "apples";
$replace_string = "oranges";
$snr = new File_SearchReplace($search_string,
$replace_string,
$files_to_search,
'', // directorie(s) to search
false) ;
$snr->doSearch();
echo "The number of replaces done : " . $snr->getNumOccurences();
The writer uses the fruits.txt file as an example.
I would like to do a search and replace on a .php file.
Basically what I am trying to achieve would be this:
On a user interaction, index.php is opened,
$promoChange = "%VARYINGTEXT%";
is searched for and replaced with
$promoChange = "$currentYear/$currentPromotion";
The $current variables will vary, hence the need to change the words inbetween the "" only.
Does anyone have any input on how this type of task could be accomplished?
If anyone knows of any tutorials relating to this subject, that too would be greatly appreciated.
Thank you!
I do have everything else figured out, regarding the template and user interaction, I am just having trouble trying to work out how to accomplish this type of search and replace. I have an understand of how it should be done as I have made something similiar using visual basic. But I am starting to this that my answer for this would be perl? I hope that this is not so...
Okay, my problem is partly solved with this:
// Define result of Activate click
if (isset($_POST['action']) and $_POST['action'] == 'Activate')
{
include ''.$docRoot.'/includes/pear/SearchReplace.php' ;
$files = array( "$docRoot/promotions/index.php" ) ;
$snr = new File_SearchReplace( '$promoChange = "";', '$promoChange = "'.$currentYear.'/'.$currentPromotion.'";', $files) ;
$snr -> doSearch() ;
}
but how do i get it to search and replace something like $promoChange = "%VARYINGTEXT%";
It found and replaced "" with the current session values. But now that is has changed, I need it to replace and text inbetween "AND".
Any ideas anyone?
If you only need to adapt a single file, then do it manually:
$src = file_get_contents($fn = "script.php");
$src = str_replace('"%VARYINGTEXT%"', '"$currentYear/$currentPromotion"', $src);
file_put_contents($fn, $src);
str_replace is sufficient for your case.
Why on earth do you want to do something like that? Frameworks like PHP do exist solely on the base of not having to write a page for each different view of the same interaction. What's wrong with just including the PHP page you now want to change, and set the variables accordingly before calling it?
Ontopic: I don't see why what you're doing is a problem, purely technically speaking. This can be done using PHP. But really, you shouldn't.
I'm trying to include a file to output in a tab on a page. The file itself will pull up just fine, but when I try to add the required querystring to it, it gives me a "failed to open stream: No such file or directory" error.
I've tried just a straight include and tried setting the querystring as a variable. Here's where I'm at right now.
$listingVars = '?mls=' . $_REQUEST['mlid'] . '&lid=0&v=agent';include("agentview.php$listingVars");
Has anyone successfully done this?
You can't include a query string in an include().
Assuming this is a local script, you could use:
$_REQUEST['mls'] = $_REQUEST['mlid'];
$_REQUEST['lid'] = 0;
$_REQUEST['v'] = 'agent';
include("agentview.php");
if it's a remote script on a different server, don't use include.
I created a variable on the 2nd page - and passed it a value on the first page - and it worked for me:
*Page with include: 'index.php'
<?php $type= 'simple'; include('includes/contactform.php'); ?>
*Page included: 'includes/contactform.php'
switch($type){
case 'simple':
//Do something simple
break;
default:
//Do something else
break;
}
I modify the accepted answer given by Frank Farmer a bit to work for different queries:
Include twice would cause problem:
$_REQUEST['mls'] = $_REQUEST['mlid'];
$_REQUEST['lid'] = 0;
$_REQUEST['v'] = 'agent';
include("agentview.php");
//changing the v to another
$_REQUEST['v'] = 'agent2';
include("agentview.php");
For those who encounter this multiple include problem, you could wrap you code inside "agentview.php" in a function:
Inside agentview.php
function abc($mls,$lid,$v){
...your original codes here...
}
file need to call agentview.php
include_once("agentview.php");
abc($_REQUEST['mlid'], 0, 'agent');
abc($_REQUEST['mlid'], 0, 'agent2');
Hope it helps someone encounter the same problem like me and thanks Frank Farmer for the great solution which saved me alot of time.