|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-06-28 12:04 UTC] christian dot gnoth at arcor dot de
Description:
------------
Hello,
I try to find a string in another string using strpos. If both the string are passed to the strpos function enclosed in ' it is working properly and the position is returned. If I put the string - in which the other string has to be find - in a variable and pass the variable to the strpos function I get FALSE.
Maybe a problem of character conversion or php configuration????
the website is: joomla-wp.it-gnoth.de
with it-gnoth.de/phpinfo.php your will see the complete configuration.
the string which is to be searched:
href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97"
the string which has to be find:
article&id=
below the php source code - both if version get the same result. It goes always in the else tree.
Test script:
---------------
function j2wp_change_single_url( $j2wp_post, $lnk_pos )
{
global $CON,
$wpdb;
$j2wp_wp_tb_prefix = get_option('j2wp_wp_tb_prefix');
$j2wp_joomla_tb_prefix = get_option('j2wp_joomla_tb_prefix');
$permalink = false;
// $lnk_pos ---> pos at href=" string in post_content
// $post_lnk_end ---> pos at last " in link string of post_content
// $post_lnk_string ---> contains the whole link string inkl. " at the end
$post_lnk_end = strpos( $j2wp_post['post_content'], '"', $lnk_pos + 7);
$post_lnk_string = substr( $j2wp_post['post_content'], $lnk_pos, $post_lnk_end - $lnk_pos + 1 );
if ( !(strrpos( $post_lnk_string, '/') === false) )
$pos_lnk_last_slash = strrpos( $post_lnk_string, '/');
// urls with structure: index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97
// if ( !(strpos( $post_lnk_string, 'article&id=') === false) )
if ( $pos_article_id = strpos( $post_lnk_string, 'article&id=') )
{
$pos_article_id += 11;
$article_id = j2wp_extract_number( substr( $post_lnk_string, $pos_srticle_id ) );
$url_post_id = j2wp_get_post_url_for_id( $article_id );
$permalink = get_permalink( $url_post_id );
}
else
{
......
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 01 14:00:02 2025 UTC |
here now the output where I do echo the strpos return: Joomla To Wordpress Migration - Status Messages The following links must be changed manually: Post ID: 105 link: href="webcasts/llpa/llpa.html" :: Post ID: 105 link: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97" :40: Post ID: 105 link: href="webcasts/llpa/llpa.html" Post ID: 106 link: href="component/search/ihda.html?ordering=&searchphrase=all" :: Post ID: 106 link: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97" :40: Post ID: 106 link: href="component/search/ihda.html?ordering=&searchphrase=all" Post ID: 107 link: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97" :: Post ID: 107 link: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97" :40: Post ID: 107 link: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97" Post ID: 287 link: href="index.php?option=com_content&view=article&id=356:qaa-fannie-mae-and-freddie-mac&catid=39:news&Itemid=70" :: Post ID: 287 link: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97" :40: Post ID: 287 link: href="index.php?option=com_content&view=article&id=356:qaa-fannie-mae-and-freddie-mac&catid=39:news&Itemid=70" Post ID: 293 link: href="index.php?option=com_content&view=article&id=331:fed-funds-recap-the-fed-oil-and-you&catid=44:dailyupdate&Itemid=96" :: Post ID: 293 link: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97" :40: here the changed script: // $lnk_pos ---> pos at href=" string in post_content // $post_lnk_end ---> pos at last " in link string of post_content // $post_lnk_string ---> contains the whole link string inkl. " at the end $post_lnk_end = strpos( $j2wp_post['post_content'], '"', $lnk_pos + 7); $post_lnk_string = substr( $j2wp_post['post_content'], $lnk_pos, $post_lnk_end - $lnk_pos + 1 ); if ( !(strrpos( $post_lnk_string, '/') === false) ) $pos_lnk_last_slash = strrpos( $post_lnk_string, '/'); $strpos_ret = strpos( $post_lnk_string, 'article&id='); echo 'Post ID: ' . $j2wp_post['ID'] . ' link: ' . $post_lnk_string . ' :' . $strpos_ret . ':<br />'; $strpos_ret = strpos( 'href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97"', 'article&id='); echo 'Post ID: ' . $j2wp_post['ID'] . ' link: ' . 'href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97"' . ' :' . $strpos_ret . ':<br />'; // urls with structure: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97" // if ( $pos_article_id = strpos( $post_lnk_string, 'article&id=') ) if ( !(strpos( $post_lnk_string, 'article&id=') === false) ) { $pos_article_id += 11; $article_id = j2wp_extract_number( substr( $post_lnk_string, $pos_srticle_id ) ); $url_post_id = j2wp_get_post_url_for_id( $article_id ); $permalink = get_permalink( $url_post_id ); } else { as you can see, PHP does not find the string in the variable and the working process!!!If you are experiencing a problem with strpos and variables, then please provide a script similar to: $haystack = "some content"; $needle = "content"; if (strpos($haystack, $needle) === false)) { echo "not found\n"; } else { echo "found\n"; } Everything else is irrelevant. However, as Rasmus said, there is no difference in strpos if you pass a static variable or a string.as I wrote I offered you the complete source as zip file and the mysql dump. then the whole php function is printed in this report. I changed the script and simplyfied the else tree: function j2wp_change_single_url( $j2wp_post, $lnk_pos ) { global $CON, $wpdb; $j2wp_wp_tb_prefix = get_option('j2wp_wp_tb_prefix'); $j2wp_joomla_tb_prefix = get_option('j2wp_joomla_tb_prefix'); $permalink = false; // $lnk_pos ---> pos at href=" string in post_content // $post_lnk_end ---> pos at last " in link string of post_content // $post_lnk_string ---> contains the whole link string inkl. " at the end $post_lnk_end = strpos( $j2wp_post['post_content'], '"', $lnk_pos + 7); $post_lnk_string = substr( $j2wp_post['post_content'], $lnk_pos, $post_lnk_end - $lnk_pos + 1 ); if ( !(strrpos( $post_lnk_string, '/') === false) ) $pos_lnk_last_slash = strrpos( $post_lnk_string, '/'); $strpos_ret = strpos( $post_lnk_string, 'article&id='); echo 'Post ID: ' . $j2wp_post['ID'] . ' link: ' . $post_lnk_string . ' :' . $strpos_ret . ':<br />'; $strpos_ret = strpos( 'href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97"', 'article&id='); echo 'Post ID: ' . $j2wp_post['ID'] . ' link: ' . 'href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97"' . ' :' . $strpos_ret . ':<br />'; // urls with structure: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97" // if ( $pos_article_id = strpos( $post_lnk_string, 'article&id=') ) if ( !(strpos( $post_lnk_string, 'article&id=') === false) ) { $pos_article_id += 11; $article_id = j2wp_extract_number( substr( $post_lnk_string, $pos_srticle_id ) ); $url_post_id = j2wp_get_post_url_for_id( $article_id ); $permalink = get_permalink( $url_post_id ); } else { echo 'Post ID: ' . $j2wp_post['ID'] . ' link: ' . $post_lnk_string . '<br />'; but still the output is the same. It still goes in the else tree. please take the time to look ofver the source which is provided here - it already shows the complete php function.Would you please do what I asked 3 times already? Make it possible for us to reproduce your problem. That means showing us your exact inputs. eg. function j2wp_change_single_url( $j2wp_post, $lnk_pos ) { global $CON, $wpdb; echo "j2wp_post = ".print_r($j2wp_post,1); // ADD THIS echo "lnk_pos = ".$lnk_pos."\n"; // ADD THIS $j2wp_wp_tb_prefix = get_option('j2wp_wp_tb_prefix'); $j2wp_joomla_tb_prefix = get_option('j2wp_joomla_tb_prefix'); echo "j2wp_wp_tb_prefix = $j2wp_wp_tb_prefix\n"; // ADD THIS echo "j2wp_joomla_tb_prefix = $j2wp_joomla_tb_prefix\n"; // ADD THIS ... And do a View-Source for the results so you don't get tricked by browser encodings. Or better yet, make a little standalone case that includes this function and pass the variables into it that is causing it to fail and put that up somewhere. I can assure you that strpos does not change based on any sort of hosting environment issues. Your problem is entirely in your code or in the data you are passing into it.echo "j2wp_post = ".print_r($j2wp_post,1); // ADD THIS echo "lnk_pos = ".$lnk_pos."\n"; // ADD THIS $j2wp_wp_tb_prefix = get_option('j2wp_wp_tb_prefix'); $j2wp_joomla_tb_prefix = get_option('j2wp_joomla_tb_prefix'); echo "j2wp_wp_tb_prefix = $j2wp_wp_tb_prefix\n"; // ADD THIS echo "j2wp_joomla_tb_prefix = $j2wp_joomla_tb_prefix\n"; // ADD THIS the last two echo are not relevant for this problem, they are used for mysql access. the first two echos are too not relevant - I am doing already an echo to the two variables I am passing to strpos!!!!!!!!!!!!!!!!!!!!!!!!!!!! the $j2wp_post is an array with differnet fields for the wordpress posts table. only the $j2wp_post['post_content'] field is intresting an this I am extracting in the variable $post_lnk_string. the variable $post_lnk_string I pass to the strpos function: To this variable I am doing an echo. I have changed the script and printing now the content of this variable with print_r as you suggest. here is the source code: echo 'Post ID: ' . $j2wp_post['ID'] . ' link: ' . print_r($post_lnk_string,true) . ' :' . print_r($strpos_ret,true) . ':<br />'; $strpos_ret = strpos( $post_lnk_string, 'article&id='); echo 'Post ID: ' . $j2wp_post['ID'] . ' link: ' . $post_lnk_string . ' :' . $strpos_ret . ':<br />'; $strpos_ret = strpos( 'href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97"', 'article&id='); echo 'Post ID: ' . $j2wp_post['ID'] . ' link: ' . 'href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97"' . ' :' . $strpos_ret . ':<br />'; // urls with structure: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97" // if ( $pos_article_id = strpos( $post_lnk_string, 'article&id=') ) if ( !(strpos( $post_lnk_string, 'article&id=') === false) ) here is the output: Post ID: 105 link: href="webcasts/llpa/llpa.html" :: Post ID: 105 link: href="webcasts/llpa/llpa.html" :: Post ID: 105 link: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97" :40: Post ID: 105 link: href="webcasts/llpa/llpa.html" Post ID: 106 link: href="component/search/ihda.html?ordering=&searchphrase=all" :: Post ID: 106 link: href="component/search/ihda.html?ordering=&searchphrase=all" :: Post ID: 106 link: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97" :40: Post ID: 106 link: href="component/search/ihda.html?ordering=&searchphrase=all" Post ID: 107 link: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97" :: Post ID: 107 link: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97" :: Post ID: 107 link: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97" :40: Post ID: 107 link: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97"The set of steps required to reproduce your problem is rather long. Hopefully you realize now why we aren't falling over ourselves reproducing your environment just to find your bug for you. How about this: Just before your strpos call, add this: file_put_contents("/tmp/debug.txt",$post_lnk_string); Then run this script: <?php $post_lnk_string = file_get_contents("/tmp/debug.txt"); var_dump(strpos($post_lnk_string, 'article&id=')); If that is still broken, you now have a small reproducing script. If you can't figure out your problem by looking at /tmp/debug.txt then zip or tar it up and make it available to use somewhere. Hint, I bet your & is actually & or you have a non-printable char in there, somewhere which you can check by doing: od -c /tmp/debug.txt