2013-03-19 123 views
0

我在服務器之間傳輸數據,並需要更改我們的wiki數據庫中的一些鏈接。我寫了一個快速的腳本,應該能夠做到這一點沒有問題。但是當我運行它時,if($datacount)條件從不運行。這可能很簡單,我錯過了,但我看不出是什麼問題。數據庫字符串在postgresql比較不識別字符串長度

$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++; 
} 

謝謝! 編輯

當我刪除了$datacount條件,這是我的輸出

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 
+0

'$ string'的內容是什麼? (與'$ datacount'一起回顯)。 – 2013-03-19 15:00:00

+0

$ string只是一些隨機文本。這很奇怪,因爲當我在'$ datacount'旁邊回顯'$ info'時,我得到了如下長度:144,'$ info' = String(144){'one441 characters of stuff'},所以'$ info'內有內容'$ count'並不總是等於0. – Ryan 2013-03-19 15:25:40

回答

1

我今天宣佈一個無腦的一天對我來說。

<?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']; 
    } 
} 

?> 
+0

直接從php strpos手冊中獲得了該部分。 http://php.net/manual/en/function.strpos.php該條件觸發,我從strpos – Ryan 2013-03-19 16:27:19

+0

@Ryan得到一個空分隔符錯誤:在更換我的大腦後更新了答案。我認爲現在是小睡的時候了。 – 2013-03-19 19:12:55