2013-12-18 15 views
0

蔭限制在字符串中的字符數:我如何使用這個腳本用mysql

$thisproductname_url=str_replace(" ", "-", $r["name"]); // replace a space with hyphen 
$thisproductname_url = preg_replace("/[^0-9a-zA-Z-]/", '', $thisproductname_url); 

但在變量$ thisproductname_url chracters的數量有時會過多(240+),並打破了鏈接。

有沒有辦法限制變量 - $ thisproductname_url - 要說200個字符?

+0

由於原始字符串是用戶輸入,那麼限制用戶可以輸入的長度是什麼? – marekful

回答

0

我不確定這與你的str_replace和preg_replace有什麼關係,但是如果你想在200個字符處截斷一個字符串,只需使用substr (manual)。如果需要,您可以先檢查長度。

+0

我使用: if(strlen($ thisproductname_url)> 200){thisproductname_url = substr($ thisproductname_url,0,-50); } –