0

I am transferring data between servers, and need to change some links in our wiki database. I wrote a quick script that should be able to do this no problem. But When I run it, the if($datacount) condition never runs. This is probably something simple that I'm missing, but I can't see what the issue is.

$dbconn = pg_connect("host= localhost port=5432 dbname=wiki user=user password=password");
$sql = "SELECT old_text FROM wiki_public.pagecontent LIMIT 10";
$go = pg_query($dbconn,$sql);
$i=0;
$string = 'sometext';
while($data = pg_fetch_assoc($go)) {
  $info = $data['old_text'];
  $count = strlen($string);
  $datacount = strlen($info);
  echo "Length: ".$datacount."<br/>";
  if($datacount != 0) {
    $position = strpos($string,$info);
    if($position !== false) {
      echo "The string ".$string." was found in row ".$i." and exists at position ".$position."The original content was: ".substr($info, $position,$count)."<br/>";
    }
  }
  $i++;
}

Thanks! EDIT

When I remove the $datacount condition, this is my output

Length: 0

Warning: strpos(): Empty delimiter in D:\www\import\linksupadte.php on line 12
Length: 12
Length: 0

Warning: strpos(): Empty delimiter in D:\www\import\linksupadte.php on line 12
Length: 31
Length: 31
Length: 0

Warning: strpos(): Empty delimiter in D:\www\import\linksupadte.php on line 12
Length: 444
Length: 1614
Length: 153
Length: 125
2
  • What is the content of $string? (echo it together with $datacount). Commented Mar 19, 2013 at 15:00
  • $string is just some random text. It's weird because when I echo $info alongside $datacount I get something like Length: 144, $info=String(144){'onehundred44 characters of stuff'}, so there is content inside $info and $count is not always equal to 0. Commented Mar 19, 2013 at 15:25

1 Answer 1

1

I'm declaring today a brain-free day for me.

<?php
# Database connection snipped.

# Let's say the database contains at least one row that's a substring
# of $string. Something like 'John', or '/123/'.
$string = 'John/123/456/789/012';
while($data = pg_fetch_assoc($go)) {

  $position = strpos($string, $data['old_text']);

  if ($position !== false) {
    echo "$string contains ".$data['old_text'];
  }
  else {
    echo "$string does not contain ".$data['old_text'];
  }
}

?>
2
  • Got that part straight from the php strpos manual. php.net/manual/en/function.strpos.php That condition fires, I get an Empty delimiter error from strpos Commented Mar 19, 2013 at 16:27
  • @Ryan: Updated answer, after replacing my brain. I think it's now time for a nap. Commented Mar 19, 2013 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.