String Comparison Issue with xml and php - php

I have a problem with xml in php... when I say echo $meta2->nodeValue; It shows Address but when I say if ($meta2->nodeValue=="Address"){echo $meta2->nodeValue;} it does not show anything... Any ideas? nodeType=1

You should trim the value to be sure to avoid all non-visible character:
if (rtrim($meta2->nodeValue) == "Address")
{
echo $meta2->nodeValue;
}

Related

Return true/false if word in URL matches specific word

I currently use:
if(strpos($command->href,§current_view) !== false){
echo '<pre>true</pre>';
} else {
echo '<pre>false</pre>';
}
$command->href will output something like this: /path/index.php?option=com_component&view=orders Whereas
§current_view is outputting orders. These outputs are dynamically generated, but the scheme will always be the same.
What I need to do is return true/false if the words from $current_view match the view=orders in the URLs from $command->href. The issue with my code is, that it doesnt match anything.
What is the correct way to do this?
Please note that the $command->href and the whole code is inside a while function, that pass multiple URLs and this only needs to match the same ones.
Breaking it down to a simple example, using your code and variable values.
$current_view = 'orders';
$command = '/path/index.php?option=com_component&view=orders';
if(strpos($command,$current_view) !== false){
echo '<pre>true</pre>';
}
else {
echo '<pre>false</pre>';
}
The oputput is "true".
Now, go and debug the REAL values of $command->href and $current_view...
I'm pretty confident that the values are not what you think they are.
Does something like:
if(substr($command->href, strrpos($command->href, '&') + 6) == $current_view)
accomplish what you are after?
To explain, strpos get the last instance of a character in a string (& for you, since you said it always follows the scheme). Then we move over 6 characters to take "&view=" out of the string.
You should now be left with "orders" == "orders".
Or do you sometimes include some arguments after the view?
Try parsing url, extracting your view query string value and compare it to $current_view
$query= [];
parse_str(parse_url($command->href)['query'], $query);
if($current_view === $query["view"])
echo '<pre>true</pre>';
} else {
echo '<pre>false</pre>';
}

php strstr not detecting substring in a arabic string

Can anyone please tell me ,How to make string matching function work with Arabic keywords?
The Following keywords are detected as Unmatched in php. Though these are matching in normal browser searching.
if (strstr('الهيئة العامة للقوى العاملة', 'قوى العامله') != false) {
echo'in';
} else {
echo'out';
}die;
Result comes as 'out'.
Thanks
Try this .. I have PHP Version 5.6.23
<?php
$string = 'الهيئة العامة للقوى العاملة';
$find_me = 'قوى العاملة';
if (strstr($string, $find_me) != false) {
echo'in';
} else {
echo'out';
}die;
?>
You can see output here https://eval.in/764085
The result for the match you are doing should be out, so it's working just fine for you. There is no match for the search you are doing since the last letter in the word العاملة is different between the two strings.
Try to re-save the page in
UTF-8 Wihtout Bom
this work for me

if string is equal to alt+0173

In facebook comment section when i type alt+0173 and press enter it submit my comment as empty comment and i want to avoid this in my website I use the following code.
if ($react == ''){
#do nothing
} else {
#insert data
}
but it didn't work and insert the data with letter "A" with two dots on the top see the below image. when i copy and past it shows as "­".
I also try the following code but it also didn't work.
if ($react == '' || $react == '­'){
#do noting
} else {
#insert data
}
I didn't verify but i think this is your solution:
alt+0173 is ascii char 173 and called Soft hyphen.
This is sometimes used to go past security scripts as you see no space but there is a char. So you can use a blocked word like bloc+173 char+ked is shown on screen as blocked but sometimes is is not picked up by the security script.
The following line prevents use of this character by removing it(it has no good use anyways).
Put it before your if/else lines.
$string = str_replace(chr(173), "", $string);
in your case:
$react = str_replace(chr(173), "", $react);
So in your case if the string only contains the alt+0173 char the string should now be empty.
Update:
But...
In your case there is something strange happening, you say your input is alt+0173 but you get an Ä which is chr(142).
Even stranger, when i asked to revert the character string to an ascii char with ord($react); you got chr(97) which is a lowercase 'a'.
As you stated you use ajax, but my knowledge of ajax is minimal so i can't help you there but maybe someone can so i hope i clarified the case a bit.
But my best guess is that something changes the value of $react when in comes from the form to the php script and you should look there.
This method helped me to solve the answer.
source: Remove alt-codes from string
$unwanted_array = array( 'Ä'=>'A' );
$react = strtr( $react, $unwanted_array );
$newreact = preg_replace("/[^A-Za-z]+/i", " ", $react);
if ($newreact == "" || $newreact == " "){
#do nothing
} else {
#insert data
}

php string comparison not working on ==

i have question on string comparison i have
if($decryptedname == $plain)
{
echo "success <br/>";
echo $_SESSION['decryptedname'];
}
in a for loop to go through a text file, decryptedname contain a string "Lee" and plain contain the data in my text files and printing line by line, since both of it contain a field with the value "Lee" i assume it should match and print success as well as the $decryptedname but it's not, below is the copy and pasted result, where lee is $decryptedname and those others are echo by $plain thru a for loop
Lee
Decrypted name is Lee
Decrypted email is mjlee181#hotmail.com
Decrypted message is testing
<?php
if (strcasecmp($decryptedname , $plain) == 0) {
echo "success <br/>";
echo $_SESSION['decryptedname'];
?>
you can try this .May it help you
There are chances for whitespace getting added when you load contents from a text file , so in that case always do a trim() and then do the comparison.
Like this..
if(trim($decryptedname) == trim($plain))
{
echo "success <br/>";
echo $_SESSION['decryptedname'];
}
or if you are trying to check if the string contains in another string... you should be doing stripos() instead.
if(stripos($decryptedname,$plain)!==false)
{
echo "success <br/>";
echo $_SESSION['decryptedname'];
}
The above mentioned problem may be caused due to the '\n' present at the end of the string.
If the string "Lee" is present at the end of the line in the text file, it may automatically be appended with '\n' in the end. So if you remove '\n' from the end it might work fine. you can use the following function to do that:
$plain=str_replace("\r\n","",$plain);

Reading php files with special tags in php

I have a file which reads as follows
<<row>> 1|test|20110404<</row>>
<<row>> 1|test|20110404<</row>>
<<row>><</row>> indicates start and end of line.I want to read line between this tags and also check whether this tags are present.
The first thing you need to do is locate the position of this "tag". The strpos() function does just that.
$tag_pos=strpos('<> 1|test|20110404<> <> 1|test|20110404<>', '<>');
if ($tag_pos===false) {
//The tag was not found!
} else {
//$tag_pos equals the numeric position of the first character of your tag
}
If these are truly lines, an efficient way to get them all is just to split on <>.
$lines=explode('<>', '<> 1|test|20110404<> <> 1|test|20110404<>');
$lines=array_filter($lines); //Removes blank strings from array
You could improve this by adding a callback function to the array_filter() call that uses trim() to remove any whitespace and then see if it is blank or not.
Edit: Great, I see that your "tags" were missing from your post. Since your start and end tags do not match, the code above will be of little use to you. Let me try again...
function strbetweenstrs($source, $tag1, $tag2, $casesensitive=true) {
$whatsleft=$source;
while ($whatsleft<>'') {
if ($casesensitive) {
$pos1=strpos($whatsleft, $str1);
$pos2=strpos($whatsleft, $str2, $pos1+strlen($str1));
} else {
$pos1=strpos(strtoupper($whatsleft), strtoupper($str1));
$pos2=strpos(strtoupper($whatsleft), strtoupper($str2), $pos1+strlen($str1));
}
if (($pos1===false) || ($pos2===false)) {
break;
}
array_push($results, substr($whatsleft, $pos1+strlen($str1), $pos2-($pos1_strlen($str1))));
$whatsleft=substr($whatsleft, $pos2+strlen($str2));
}
}
Note that I haven't tested this... but you get the generally idea. There is probably a much more efficient way to go about doing it.
Creating your own format is not so hard, but creating a script to read it can be difficult.
The advantage of using standardized formats is that most programming languages has support for them already. For example:
XML: You can use the simplexml_load_string() function and it can make you navigate easily through your content.
$str = "<?xml version="1.0" encoding="utf-8"?>
<data>
<row>1|test|20110404</row>
<row>1|test|20110404</row>
</data>";
$xml = simplexml_load_string($str);
Now you can access your data
echo $xml->row[0];
echo $xml->row[1];
i'm sure you get the idea,
there is also a very good support for JSON (Javascript Object Notation) using the jsondecode() function;
Check it on php.net for more details
i would suggest to use preg_match :-
preg_match( '#<< row>>(.*)<< /row>>#', $line, $matches);
if( ! empty($matches))
{
// line was found
print_r( $matches[1] ); // will contain the content between the start and end row tags
}

Categories