2011-10-05 60 views
1

所以我是PHP的新手,我在修剪功能方面遇到了一些困難。使用MySQL獲取數據後的PHP修剪

基本上我需要

這裏是我的源代碼。

<?php 

include "../include/setup.inc.php"; 


$query="SELECT offer_id,title,email,phone,site,zip,price,return_customer,request_date,traffic_source,agent_email,released FROM jos_quote 
    where request_date >= '2011-10-04' order by request_date"; 
$result = mysql_query($query);   
    if (!$result) {echo "<p><b>".$query ."</b></p>";echo 'MySQL Error: ' . mysql_error(); return;} 
$num_rows = mysql_num_rows($result); 
    echo '<br><table border="1" align="center" width="700px"> 
    <tr bgcolor="#FF8000"><td colspan="11" align="center">Total: '.$num_rows.'quotes - Date Range: '.$_POST['date1'].' -- '.$_POST['date2'].' </td></tr> 
    <tr><th>Title</th><th>Email</th><th>Site</th><th>Zip</th><th>Price</th> 
<th>Return Customer</th><th>Request Date</th><th>Agent</th><th>Released</th><th>Traffic Source</th>'; 
while ($row = mysql_fetch_assoc($result)) { 
    echo '<tr> 
    <td>'.$row['title'].'</td> 
    <td>'.$row['email'].'</td> 
    <td>'.$row['site'].'</td> 
    <td>'.$row['zip'].'</td> 
    <td>'.$row['price'].'</td>        
    <td>'.$row['return_customer'].'</td>         
    <td>'.$row['request_date'].'</td>         
    <td>'.$row['agent_email'].'</td>          
    <td>'.$row['released'].'</td>         
    <td>'.$row['traffic_source'].'</td>             
    </tr>'; 
} 
echo '</table>'; 
$query="select agent_email,num_quote,num_receive,set_date from quote_distro where set_date >= '2011-10-04'"; 
$result = mysql_query($query);   
    if (!$result) {echo "<p><b>".$query ."</b></p>";echo 'MySQL Error: ' . mysql_error();  return;} 
    while ($row = mysql_fetch_assoc($result)) { 
$quote_info[$row['agent_email']][$row['set_date']] =$row; 

} 


} 


?> 

回答

1

這不是修剪()函數所做的事情。 trim()去除字符串開頭和結尾的空白。

我需要從所有agent_email值中修剪@ example.com。

您可以使用str_replace。例如,要從字符串中刪除@ example.com。

$string = str_replace ('@example.com', '', $string); 

相反,如果你想刪除的任何後at符號(@),您可以使用爆炸:

list($string) = explode('@', $string); 

我還需要向下修整traffic_source 30個字符

這又是另一個概念。您可以使用PHP函數SUBSTR爲:

$string = substr($string, 0, 30); 

如果你想只添加省略號標誌當字符串被截斷:

if (strlen($string) > 30) { 
    $string = substr($string, 0, 30) . '&hellip;'; 
} 
0
substr($row['traffic_source'], 0, 30); 
str_replace('@example.com', '', $row['agent_email']); 
0

我會做,在MySQL中,而不是PHP。交通源的情況是如此簡單:

SELECT LEFT(traffic_source, 30) as traffic_source FROM (...) 

對於@ example.com,您可以使用REPLACE

SELECT REPLACE(email, '@example.com', '') AS email (...) 

您還可以使用SUBSTRING

SELECT SUBSTRING(email, 0, INSTR(url, '@')) AS email 

這些和更多的字符串函數被記錄在here