php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52198 strpos not working correctly
Submitted: 2010-06-28 12:04 UTC Modified: 2010-07-05 17:51 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: christian dot gnoth at arcor dot de Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.2.13 OS: Linux
Private report: No CVE-ID: None
 [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
  {
......


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-28 15:41 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2010-06-28 15:41 UTC] rasmus@php.net
Based purely on your bug description, you are saying this doesn't work?

<?php
$haystack = 'href="index.php?option=com_content&view=article&id=9257:2009-fha-
loan-limits&catid=52:fha&Itemid=97"';
$needle = 'article&id=';
echo strpos($haystack,$needle);

When I run this I get: 40
which looks correct.

Please simplify your reproduce code and I bet you will find that your mistake is 
elsewhere.
 [2010-06-28 16:01 UTC] christian dot gnoth at arcor dot de
yes it does not work !!!

I get the result 40 if I put it directly as string in the function, but in the variable it does not work.

Here the browser output:

Joomla To Wordpress Migration - Status Messages




The following links must be changed manually: 

Post ID: 105 link: href="webcasts/llpa/llpa.html"
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: 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"

As you can see the Post ID 107 there is the string, but the output is generated in the else tree of the if statement.

the whole script you can find under:

http://joomla-wp.it-gnoth.de/wp-content/plugins/joomla2wp/


here the script part:

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: href="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
  {
    //  urls with structure: /82345-fha-loan-limits
    //                   or  href="mortgagecenter/39-news/11548-focus-on-the-6500-tax-credit.html"
    $itemid = j2wp_extract_number( substr( $post_lnk_string, $pos_lnk_last_slash + 1 ) );   
    //  itemid is there - look it joomla for title and creation,modified date    
    if ( $itemid )
    {
      $url_post_id = j2wp_get_post_url_for_id( $itemid );

      $permalink = get_permalink( $url_post_id );
    }
    else
    {
      //  urls with structure: all other
      // check
      // index.php?option=com_content&view=section&id=9&layout=blog&Itemid=64
      // index.php?option=com_content&view=category&id=49:credit-optimization&layout=blog&Itemid=78

      //  it is a category or .html or attachment file
      $link_string        = substr( $post_lnk_string, 7, strlen( $post_lnk_string ) - 8);
      $pos_lnk_last_slash = strrpos( $link_string, '/'); 
    
      //  check if is a category page
      if (  !strpos($link_string, '.') )
      {
        $joomla_cat_title = NULL;
        // determine the slug 
        if ( !$pos_lnk_last_slash )
        {
          $cat_slug = $link_string;
        }
        else
        {
          $cat_slug = substr( $link_string, strrpos( $link_string, '/') + 1);
        }
        j2wp_do_joomla_connect();
        echo 'Post ID: ' . $j2wp_post['ID'] . ' link: ' . $post_lnk_string . ' cat_slug:' . $cat_slug . '<br />'; 
        // Get the ID of a given category from Joomla
        $query =  'SELECT id, title, alias FROM ' . $j2wp_joomla_tb_prefix . 'categories WHERE alias = "' . $cat_slug . '" ';
        echo $query . '<br />';
        $result = mysql_query($query, $CON);
        if ( !$result )
          echo mysql_error();
          
        while( $row = mysql_fetch_array($result) ) 
        {
          $joomla_cat_id = $row['id'];
          $joomla_cat_title = $row['title'];
        }
        if ( $joomla_cat_title )
        {
          echo 'Cat: ' . $joomla_cat_title . '<br />';
          $category_id = get_cat_ID( $joomla_cat_title );
          // Get the URL of this category
          $permalink = get_category_link( $category_id );
          echo 'Cat_link: ' . $permalink . '<br />';
        }
      }
      else
      {
        //  check if there is a '.' inside the $last_string and not .html - then it is an attachment
        //  strrpos($last_string, '.')
        echo 'Post ID: ' . $j2wp_post['ID'] . ' link: ' . $post_lnk_string . '<br />'; 
      }
    } 
  }
 [2010-06-28 16:07 UTC] christian dot gnoth at arcor dot de
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!!!
 [2010-06-28 16:08 UTC] rasmus@php.net
The test script I provided doesn't work?
 [2010-06-28 17:08 UTC] christian dot gnoth at arcor dot de
as you can see the strpos function works also in my script it returns the position 40, but only in the static passing of the strings. In the work process - the function is called for each url - somehow the variables are not processed correctly (maybe some problem in the memory areas???). The content of the variables is shwon correctly through the echo construct, but the content is not processed correctly by the strpos function.
 [2010-06-28 17:15 UTC] rasmus@php.net
Your bug is elsewhere.  There is no difference between passing a static string and 
passing a variable.  The only difference happens if the contents of the variable 
is different.  Write a 5-line script that reproduces the problem and you will see.
 [2010-06-28 18:20 UTC] christian dot gnoth at arcor dot de
Hello,
that 5 lines are working - I do not doubt. That is not the problem here. Again, strpos is working with static strings passed, but with variables.

That is a fact as you can see it. The output is generated at the only position in the else tree!!!! So with strpos working correctly it should never go there for that string!!!

If you like, I can send you the MySQL dump file and the php script.
 [2010-06-28 18:26 UTC] pajoye@php.net
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.
 [2010-06-28 18:58 UTC] rasmus@php.net
The test script I showed you used variables, not static strings.

My guess (since I can't decipher your messy code) is that your POST data is 
probably getting parsed or filtered and the contents isn't what you think it is.  
Your & might actually be &amp; so the strpos isn't going to match.  Look for 
issues along those lines.  strpos works fine, so the longer you keep blaming 
strpos the longer it will take you to find your bug.
 [2010-06-28 19:10 UTC] christian dot gnoth at arcor dot de
of what use is it to proove something what is working??? 

That there is no difference is not true!!!

if I pass a constant string always each function call it is different than to pass variable which content is with each call different. Means the memory for the static string remains always equal with each call, but the memory area of the variable is always differnet - in place (I think with each call the variable is allocated again, or?) and in content.

So please take the time and look through the code I provided - you can see clearly that the output is generated at the end of the else tree in which the PHP interpreter should never go!!! 
The output is generated of the variable $post_lnk_string

echo 'Post ID: ' . $j2wp_post['ID'] . ' link: ' . $post_lnk_string . '<br />';

the if:

if ( !(strpos( $post_lnk_string, 'article&id=') === false) )

should not allow that to happen!!!!!

and as you can see in the output:

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"

from the source code:

$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) )


the problem is not with the if, but with the strpos function !!!!

Please take the time and look through this code examples and the generated output!!!
 [2010-06-28 19:13 UTC] rasmus@php.net
No, you need to take the time to simplify your case into a standalone test that 
proves your point instead of asking us to weed through a lot of code.  There is 
absolutely nothing wrong with the strpos function, so the exercise of producing a 
standalone test will be enlightening for you.  You can choose not to believe me, I 
suppose, that is entirely up to you.
 [2010-06-28 19:21 UTC] rasmus@php.net
By the way, even if I was inclined to look through your code, you haven't provided 
it.  The http://joomla-wp.it-gnoth.de/wp-content/plugins/joomla2wp/ link is 
useless since it doesn't serve up a visible version of the scripts there.
 [2010-06-28 19:52 UTC] christian dot gnoth at arcor dot de
what will be enlightening to me if I know that the strpos function is working in a simple script and with a static string passed, but not in big script with different function calls?

What I have if I proove the strpos is working, but in the that case not?

I provided the source code here. In the directory you will find the .php files which you can download - I see in the browser a direcotry list.

f you want I can provide a MySQL dump and the php scripts as .zip file. But the source code is above of the whole function.
 [2010-06-28 19:59 UTC] rasmus@php.net
Sorry, but you have PHP configured to parse files in that directory, so nobody 
can download your source code.  Try it for yourself.  

And what simplifying your problem will do is show you that it isn't a strpos 
issue.  Start with your complex example that isn't doing what you expect.  
Simplify it a little bit and see if it is still "broken".  Keep doing that until 
it starts working and you will have found your bug.

So far you have done nothing but waste our time.  You refuse to provide a simple 
reproducing script and you also have not provided a way to get your complicated 
reproducing script.
 [2010-06-28 21:12 UTC] christian dot gnoth at arcor dot de
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.
 [2010-06-28 21:19 UTC] rasmus@php.net
That function is not a standalone test case.  It references global variables and 
calls other functions like get_option, j2wp_extract_number, 
j2wp_get_post_url_for_id and a bunch of others.  Why in the world can't you make a 
simplified standalone case that hardcodes some of these to whatever you have them 
set to and makes it very clear what you are passing to this function?

Until you do that, you will get no help from here.  I assure you that this is not 
a PHP bug.
 [2010-06-29 09:15 UTC] christian dot gnoth at arcor dot de
Hello,

that a standalone case is working or not is not the point here. Again I do not doubt that the strpos function is working in a different environment and test case. 
In the whole script it is working on different positions where I use this strpos function!!!

But here in this working process it is not working !!!

$strpos_ret = strpos( $post_lnk_string, 'article&id=');
echo 'Post ID: ' . $j2wp_post['ID'] . ' link: ' . $post_lnk_string . ' :' . $strpos_ret . ':<br />'; 

In this two lines you can see that the variable $post_lnk_string is passed to the strpos function. The echo after that shows the variable content and the return of the strpos function. The standalone test case is there !!! 

The output:

Post ID: 107 link: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97" ::

shows the content of the variable with which it was passed to the strpos function. Also it shows the return_value of the strpos function.

So, to tell it is not a PHP problem is not true - if the same script with the same data is running in your environment without this problem - then I think it is a problem of the environment which the webhoster provides.
 [2010-06-29 11:12 UTC] rasmus@php.net
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.
 [2010-07-01 03:31 UTC] anon at anon dot com
This is hilarious.
 [2010-07-04 15:11 UTC] christian dot gnoth at arcor dot de
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"
 [2010-07-04 17:44 UTC] premiso at gmail dot com
Christian, they have PHP Help forums and Joomla forums for you to use to assist you 
with your issue.

Clearly this is NOT a PHP bug more over it is just you being an incompetent 
"programmer". Please take this to a help forum, googling "php help forum" will bring up 
plenty of places that will be more than happy to help you debug your code.
 [2010-07-04 17:52 UTC] christian dot gnoth at arcor dot de
Hello,
thank you for your suggestion, but as I reported it as a PHP bug, please take it seriously and look for the reason that the strpos function do not return the postiion of the string 'needle', even if the string 'needle' is contained in the string 'haystack'.
 [2010-07-04 19:09 UTC] mchl at flingbits dot com
Let me get this straight.

strpos() is used in thousands of applications around the world, and none are experiencing this problem except yours. You claim therefore that this is a PHP bug. 

I'm sorry, but the odds are against you.

You also say, that because you reported it as a PHP bug it should be treated as such.

I'm sorry again, but it doesn't work this way. Until you can convince us that the problem is not with YOUR code, it will be treated as a bogus report.

You can convince us by providing a simple code, that reproduces this behaviour, but does not rely on external resources like database or unknown global variables. If you can't produce such code, it's indicating you're looking in the wrong place.

Personally I would advise using var_dump() before strpos() to see if haystack actually contains needle.
 [2010-07-04 19:46 UTC] christian dot gnoth at arcor dot de
Hello,
I can not follow you how you come to the idea not to analyze the given source code of a bug report, simply because you do not want use your mind to understand the logic of the source code.

For this report I made the echos in the source code that you can see the content of the variable passed to the strpos function.

As I wrote many times, I do not doubt that the strpos function is working in a different case and as I wrote it is working in different places in my sourcecode. BUT !!!! As you can see strpos is not able to find:

'article&id='
in
href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97"

in the cylcle of many nested function calls.

So - it is not a question of "strpos is working or not", but as I think a question of memory areas which may be not correct filled, passed, handled.....

And for that problem it is of no use to create other circumstances where this problem is not happening.
 [2010-07-04 20:09 UTC] rasmus@php.net
We don't want you to give us a case where it doesn't happen.  We want you to come 
up with a simpler version of your code where the problem *does* happen.  And we 
have looked at the source code you have provided and there is nothing visually 
wrong with it.  Without an actual reproducing case, there is nothing we can do, 
and you still have not provided that.
 [2010-07-04 21:36 UTC] christian dot gnoth at arcor dot de
to reproduce this case is not simple, the script is part of a wordpress plugin and runs within the wordpress environment with mysql access. It is a Joomla to Wordpress Migrator. It reads the Joomla tables and inserts the entries in the wordpress posts table. After that the URL's within the posts can be changed and pointed to the new wordpress posts.

I have provided the source code and the mysql table structure under:

http://files.it-gnoth.de/download/php_report/

To have the same environment for this case you have to do the following steps:

- install wordpress (I have the multisite feature enabled)
- import the sql file I proved into the mysql database - it contains the joomla content entries
- install the plugin in wordpress
- fill up the mysql connection parameters in the plugin admin panel
- start the migration of the joomla content to wordpress
- start the url change proccess
 [2010-07-04 21:42 UTC] pajoye@php.net
Please understand that strpos has absolutely no idea where the strings you pass it come from (ich meine es: echt keine Ahnung).

What is so hard to understand to actually do what Rasmus has been asking for a week now? Provide a script like the one I wrote in my previous comment.

If you can't reproduce it using such a simple script, then something is wrong somewhere else in your code.

If you keep need support, I would suggest to go to either the php-general mailing or to whatever is the support channel for Wordpress. But bugs.php.net is not such channel.

Thanks for your understanding.
 [2010-07-04 22:40 UTC] christian dot gnoth at arcor dot de
Hello,
thank you for your qualified answer. But, please take a look at the following lines:

$strpos_ret = strpos( $post_lnk_string, 'article&id=');
echo 'Post ID: ' . $j2wp_post['ID'] . ' link: ' . $post_lnk_string . ' :' . $strpos_ret . ':<br />'; 

and here is the output:

Post ID: 107 link: href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97" ::

So the strpos function has no idea from were the input is comming. Infact it is for the problem not intresting. Question is, from which memory area it gets the input and what is there and how it is moved to the strpos function. 
The variable which is passed to strpos contains the string 'article&id=', but this string is not found by the strpos function. 

So it is a bug in the PHP interpreter!!!

As you are the developer you should see that!!!
 [2010-07-04 23:08 UTC] pajoye@php.net
Ok.

Using:

$post_lnk_string = 'href="index.php?option=com_content&view=article&id=9257:2009-fha-loan-limits&catid=52:fha&Itemid=97"';
$strpos_ret = strpos($post_lnk_string, 'article&id=');
var_dump($strpos_ret);

This script is exactly what you are doing. The output is:

int(40)

It works.

Your analyze about the memory are is wrong. Please check the implementation if you like to verify it by yourself.
 [2010-07-05 04:10 UTC] rasmus@php.net
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 &amp; or you have a non-printable char in there, 
somewhere which you can check by doing: od -c /tmp/debug.txt
 [2010-07-05 04:45 UTC] me at whatever dot com
Christian,
once you do the echo on $post_lnk_string, use the View Source from the browser.
Your links are not &, they are &amp;

This is what I found in your SQL dump for the "link 107":

index.php?option=com_content&amp;view=article&amp;id=9257:2009-fha-loan-limits&amp;catid=52:fha&amp;Itemid=97

See... there is no "article&id=" there.
 [2010-07-05 14:02 UTC] christian dot gnoth at arcor dot de
Ok, thank you for the hint. Indeed it is an &amp; !!!
 [2010-07-05 17:51 UTC] rasmus@php.net
Right, which is what I suggested on June 28, the day you 
reported the bug:

  My guess (since I can't decipher your messy code) is that 
  your POST data is probably getting parsed or filtered and
  the contents isn't what you think it is. Your & might
  actually be &amp; so the strpos isn't going to match.

Next time, please listen to us to avoid wasting everyones' 
time.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Feb 29 06:01:28 2020 UTC