Incorrect characters in output - xpath

I'm trying to learn web scraping with Xpath. The code below works, however the output contains of incorrect characters and I can't manage to get this right.
Example:
Output: Emåmejeriet
How it should be: Emåmejeriet
PHP Code:
<?php
// Tried with these parameters but they doesn't make any difference
$html = new DOMDocument('1.0', 'UTF-8');
$html->loadHtmlFile('http://thesite.com/thedoc.html);
$xpath = new DOMXPath($html);
$nodelist = $xpath->query("//table");
foreach ($nodelist as $n) {
echo $n->nodeValue."\n";
}
?>
What can I do to fix this?

You should try encode() & decode() php functions if using ISO8859-15 or iconv() if not.
Example :
<?php
iconv_set_encoding("internal_encoding", "UTF-8");
iconv_set_encoding("output_encoding", "ISO-8859-1");
?>

Related

Put JavaScript variable into PHP with AJAX?

Here's a some of my JavaScript
function myFunction() {
var finalMove = "a1";
$.post("index.php", {postFinalMove:finalMove});
];
Here is my PHP code. The commented lines are just two of the things I've tried.
<?php
session_start();
$db = mysqli_connect("localhost","myUsername","myPassword","abraDB");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//if (isset($_POST['postFinalMove']))
//{
// $turn = ($db, $_POST['postFinalMove']);
// echo $turn;
//}
//if ($_POST['postFinalMove'])
//{
// $turn = ($db, $_POST['postFinalMove']);
// echo $turn;
//}
if ($_POST['logout'])
{
session_start();
$_SESSION = array();
session_destroy();
}
mysqli_close($db);
?>
As soon as I uncomment some of the code I've tried, my entire webpage is blank with no errors and no source code, which makes this hard for me to debug. Javascript function fires just fine, its my index.php page that crashes. PHP code was working great until this. Thanks in advance
You should be able to do the following in your PHP script:
if(isset($_POST['postFinalMove'])){
echo $_POST['postFinalMove'];
}
This line is probably causing the error:
// $turn = ($db, $_POST['postFinalMove']);
That statement does not make any sense. What are you trying to store in $turn? If you want to store the $_POST['postFinalMove'] you would have:
$turn = $_POST['postFinalMove'];
Take a look at the GIF image and you will see for yourself..!
Replace your code with this line as and you are good to go then :
$turn = $_POST['postFinalMove'];
echo $turn;
It's just a simple Syntax Issue nothing more than that Dear..! :D

Add parameter to Codeigniter URL

I have a problem when i try to add other parameter to URL.
before i use Codeigniter i add those parameters using JavaScript like this
test
but when i tried to do it with Codeigniter i don't know how.
<?php echo anchor("home/index/param1","test"); ?>
as i said i want to add this parameter for example my URL looks like this
home/index/param2
so when i click on test i want the URL to be like this
home/index/param2/param1
Take a look at CodeIgniter's URL Helper Documentation
The first parameter can contain any segments you wish appended to the URL. As with the site_url() function above, segments can be a string or an array.
For your example, you could try:
<?php
$base_url = 'home/index/';
$param1 = 'param1';
$param2 = 'param2';
$segments = array($base_url, $param1, $param2);
echo anchor($segments,"test");
?>
You can't do that with the form helper, you have to use your js function again :
echo anchor("home/index/param2", "test", array("onClick" => "javascript:addParam(window.location.href, 'display', 'param1');"));
It will produce :
test
But I don't see the point of dynamically change the href on the click event. Why don't you set it directly at the beginning ?
echo anchor("home/index/param2/param1", "test");

Laravel View::render() gives error

I'm trying to render a view with čćžšđ letters in it, but it's failing and giving me this error:
The Response content must be a string or object implementing __toString(), \"boolean\" given."
I extracted a partial, and when I load this partial using normal way (with #include) then everything is ok.
But I need "load more" function, so I'm using this partial to render html for me, and I only append it to DOM using jQuery.
This is the partial:
<?php $flag = true; ?>
#foreach($tags as $tag)
#if(empty($tag->tag)) <?php continue; ?> #endif
#if($flag == true)
<?php $temp = $tag->tag[0]; ?>
<br><label class="firstLetterLabel">{{ strtoupper($temp) }}</label><br><hr><br>
<?php $flag = false; ?>
#endif
#if($temp != $tag->tag[0])
<br><label class="firstLetterLabel">{{ strtoupper($tag->tag[0]) }}</label><br><hr><br>
#endif
<div class="singleTag">
{{ $tag->tag }}
</div>
<?php $temp = $tag->tag[0]; ?>
#endforeach
This is how I use it in "load more" function:
$tags = Tag::orderBy("tag")->take(Config::get("settings.num_tags_per_page"))->skip(($page-1)*Config::get("settings.num_tags_per_page"))->get();
$data = array();
$data['data'] = View::make("discover.partials.tags")->with("tags", $tags)->render();
if(count($tags)%Config::get("settings.num_tags_per_page") == 0 && count($tags) > 0)
$data['msg'] = 'ok';
else
$data['msg'] = 'stop';
return json_encode($data);
This partial read tags and sort them alphabetically, and it extracts first letter of every tag because I need that letter somewhere else.
And when this partial finds one of these letters čćžšđ then it gives me above error.
How to solve this?
Try this:
json_encode($output, JSON_UNESCAPED_UNICODE)
For future notice, I solved this problem by replacing $tag->tag[0] with mb_substr($tag->tag, 0, 1). This command extracts utf-8 characters from string while my previous approach wasn't properly encoding utf-8 chars.
More info here: Get first character of UTF-8 string

CodeIgniter -> Get current URL relative to base url

Tried URI::uri_string() but can't get it to work with the base_url.
URL: http://localhost/dropbox/derrek/shopredux/ahahaha/hihihi
Returns: dropbox/derrek/shopredux/ahahaha/hihihi
but http://localhost/dropbox/derrek/shopredux/ just returns an empty string.
I want the first call to return "ahahaha/hihihi" and the second to return "". Is there such a function?
// For current url
echo base_url(uri_string());
If url helper is loaded, use
current_url();
will be better
Try to use "uri" segments like:
$this->uri->segment(5); //To get 'ahahaha'
$this->uri->segment(6); //To get 'hihihi
form your first URL...You get '' from second URl also for segment(5),segment(6) also because they are empty.
Every segment function counts starts form localhost as '1' and symultaneous segments
For the parameter or without parameter URLs Use this :
Method 1:
$currentURL = current_url(); //for simple URL
$params = $_SERVER['QUERY_STRING']; //for parameters
$fullURL = $currentURL . '?' . $params; //full URL with parameter
Method 2:
$full_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Method 3:
base_url(uri_string());
I see that this post is old. But in version CI v3 here is the answer:
echo $this->uri->uri_string();
Thanks
//if you want to get parameter from url use:
parse_str($_SERVER['QUERY_STRING'], $_GET);
//then you can use:
if(isset($_GET["par"])){
echo $_GET["par"];
}
//if you want to get current page url use:
$current_url = current_url();
Running Latest Code Igniter 3.10
$this->load->helper('uri'); // or you can autoload it in config
print base_url($this->uri->uri_string());
I don't know if there is such a function, but with $this->uri->uri_to_assoc() you get an associative array from the $_GET parameters.
With this, and the controller you are in, you know how the URL looks like.
In you above URL this would mean you would be in the controller dropbox and the array would be something like this:
array("derrek" => "shopredux", "ahahaha" => "hihihi");
With this you should be able to make such a function on your own.
In CI v3, you can try:
function partial_uri($start = 0) {
return join('/',array_slice(get_instance()->uri->segment_array(), $start));
}
This will drop the number of URL segments specified by the $start argument. If your URL is http://localhost/dropbox/derrek/shopredux/ahahaha/hihihi, then:
partial_uri(3); # returns "ahahaha/hihihi"
you can use the some Codeigniter functions and some core functions and make combination to achieve your URL with query string.
I found solution of this problem.
base_url($this->uri->uri_string()).strrchr($_SERVER['REQUEST_URI'], "?");
and if you loaded URL helper so you can also do this current_url().strrchr($_SERVER['REQUEST_URI'], "?");
<?php $currentMenu = $this->uri->segment(2); ?>
<ul>
<li class="nav-item <?= ($currentMenu == 'dashboard') ? 'active' : '' ?>">
<a href="<?= site_url('/admin/dashboard'); ?>" class="nav-link"><i data-
feather="pie-chart"></i> Dashboard</a>
</li>
</ul
this is work for me

can I use joomla's onAfterRender for a module rather than a plugin?

I want to insert some code to Joomla when any page is loaded.
For this I created a module that inserts code.
I am trying to use
<?php
// $Id: helper.php
defined('_JEXEC') or die;
jimport( 'joomla.plugin.plugin' );
jimport( 'joomla.environment.response' );
class modInsertCode
{
function onAfterRender($params)
{
$code = 'some code';
$documentbody = JResponse::getBody();
$documentbody = str_replace ("</body>", $code." </body>", $documentbody);
JResponse::setBody($documentbody);
return true;
}
}
?>
but JResponse::getBody(); returns an empty string. Any ideas, solutions of fixes to this code?
Thank you,
You have to do it using a plugin, you won't be able to do it using a module because the HTML response has not been generated by the time the code of the module gets executed.
I hope it helped!
I know this is a bit old but for future reference this can be done with jQuery:
$doc = JFactory::getDocument();
$js = 'jQuery(document).ready( function() {
jQuery("#module'.$module->id.'").appendTo(document.body);
})';
$doc->addScriptDeclaration($js);
This is assuming that you have wrapped the content in you module in something like the following, including the module id to support multiple instances of the module.
<div id="module<?php echo $module->id; ?>"> Your content </div>

Resources