An arbitrarily different output of a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mirax33
Forum Newbie
Posts: 1
Joined: Thu May 01, 2008 1:51 am

An arbitrarily different output of a function

Post by mirax33 »

Hello.

I have created a function to extract text in brackets that follows a "<b>" tag in a text to be a html code as follows :

Code: Select all

 
function ExtractN($tt)
{
  $l = "";
  $tt = " ".$tt;
 
  while (strpos($tt, "<b> ") != false)
  {
    $tt = " ".substr($tt, strpos($tt, "<b> ") + 2);  
    $l = $l.substr($tt, strpos($tt, "(") + 1, strpos($tt, ")") - strpos($tt, "(") - 1).", ";
  }
  return $l;
}
 
The argument is always the same text, since it is fetched from a database and not altered thereupon - prior to being sent to the function.

The issue is that ever and anon - with no pattern that I am able of perceiving - the function returns a correct first entry with the rest being incorrect.

The format of the text that is used as an argument is :

- *A Short Text* being a text of less than 20 characters

Code: Select all

 
 
 ... some HTML tags ... 
 
<b> *A Short Text* (*A Short Text*)</b>
 
A Longer Text with some HTML tags, but no "<b>" at all
 
<b> *A Short Text* (*A Short Text*)</b>
 
A Longer Text with some HTML tags, but no "<b>" at all
 
<b> *A Short Text* (*A Short Text*)</b>
 
A Longer Text with some HTML tags, but no "<b>" at all
 
... et cetera ... some HTML tags ... 
 
The expected result is "A Short Text, A Short Text, A Short Text, A Short Text, "
.. but sometimes it is "A Short Text, A text from some point of the text, another of that kind, yet another like that"

I am utterly at loss of fiuring out what is the cause of this seeminly arbitrary behaviour of the script and I should like to ask anyone who knows the reason, to inform me. Thank you very much - in advance.
Post Reply