Edit report at http://bugs.php.net/bug.php?id=52198&edit=1

 ID:               52198
 Comment by:       christian dot gnoth at arcor dot de
 Reported by:      christian dot gnoth at arcor dot de
 Summary:          strpos not working correctly
 Status:           Bogus
 Type:             Bug
 Package:          Strings related
 Operating System: Linux
 PHP Version:      5.2.13

 New Comment:

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!!!


Previous Comments:
------------------------------------------------------------------------
[2010-06-28 16:01:02] 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 15:41:52] ras...@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 12:04:39] 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

  {

......



------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=52198&edit=1

Reply via email to