PHP การเปลี่ยนช่องว่างเป็น ขีด ช่วยทีค่ะ โดย มายองเนสจัง

เริ่มโดย มายองเนสจัง, 26 ตุลาคม 2012, 17:14:55

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

มายองเนสจัง

ติดปัญหาบางอย่างค่ะ อยากแก้คำให้ช่องว่างกลายเป็นขีดแทน ทำยังไงหรอคะ เช่น

Mayong So Much ให้กลายเป็น Mayong-So-Much อะไรประมาณนี้อะ

แล้วถ้าเขียนฟอร์ม รับคำแล้วแสดงผล ออกมานี่ยากรึเปล่าคะ แนะนำด้วยค่ะ  :wanwan009:

joetiaw

ใช้ str_replace ไม่ได้เหรอครับ  :P

$text ="Mayong So Much";
echo str_replace(" ", "-", $text);
[direct=http://www.bestproductsthai.com]Best Products Thai-เครื่องเทศและสมุนไพร[/direct]|[direct=http://www.besttelevisionreview.com/]Best Television Review[/direct]|[direct=http://www.plantsmemo.com]Plants Memo[/direct]|[direct=http://www.พันธุ์ไม้.com]ไม้ดอกไม้ประดับ[/direct]

chaikrit


$text="Mayong So Much";
echo str_replace(" ","-","$text");


ลองดูครับ เพิ่มเติมที่ลิงค์ด้านล่าง
hปปp://php.net/manual/en/function.str-replace.php

seowizard

น่าจะประมาณนี้ครับ


<form name="myform">
<input type="text" name="var1" size="25"><br>
<input type="submit" value="Submit">
</form>

รับค่าหลังจาก submit

$str_var = $_POST['var1'];
$str_var = str_replace(" ", "-", $str_var);

echo $str_var;

joetiaw

[direct=http://www.bestproductsthai.com]Best Products Thai-เครื่องเทศและสมุนไพร[/direct]|[direct=http://www.besttelevisionreview.com/]Best Television Review[/direct]|[direct=http://www.plantsmemo.com]Plants Memo[/direct]|[direct=http://www.พันธุ์ไม้.com]ไม้ดอกไม้ประดับ[/direct]

joetiaw

[direct=http://www.bestproductsthai.com]Best Products Thai-เครื่องเทศและสมุนไพร[/direct]|[direct=http://www.besttelevisionreview.com/]Best Television Review[/direct]|[direct=http://www.plantsmemo.com]Plants Memo[/direct]|[direct=http://www.พันธุ์ไม้.com]ไม้ดอกไม้ประดับ[/direct]

kungbest

อ้างถึงจาก: chaikrit ใน 26 ตุลาคม 2012, 17:18:24

$text="Mayong So Much";
echo str_replace(" ","-","$text");


ลองดูครับ เพิ่มเติมที่ลิงค์ด้านล่าง
hปปp://php.net/manual/en/function.str-replace.php
ถ้าจะเปลี่ยนแบบดื้อๆ ก็ตามนี้คับ ใช้ฟังก์ชั่น str_replace

ส่วนจะเอาไปประยุกต์ก็ต้องดูว่าจะใช้งานแบบไหนคับ
:wanwan020:

มายองเนสจัง

ทำได้ละค่ะ ทำแบบมั่วๆ เอาแบบนี้


<?php
$word 
"$_POST[message]";
$newVariabe preg_replace('/\s+/''-'$word);  
echo 
"<div class='cutworld'>";
echo 
"$newVariabe";
echo 
"</div>";
?>




ขอบคุณที่แนะนำค่ะ  :wanwan006:

ThaiMagic


BeerKingMan


artmond

ไม่ทราบว่าจะนำไปใช้เป็น Slug หรือเปล่าครับ

ยังไงลองตรวจสอบรูปแบบให้ดีนะครับ เพราะโดยปกติมันจะมีอักขระพิเศษอื่นๆ อีกที่เราต้องแปลง

ohmohm

อ้างถึงจาก: มายองเนสจัง ใน 26 ตุลาคม 2012, 17:29:05
ทำได้ละค่ะ ทำแบบมั่วๆ เอาแบบนี้


<?php
$word 
"$_POST[message]";
$newVariabe preg_replace('/\s+/''-'$word);  
echo 
"<div class='cutworld'>";
echo 
"$newVariabe";
echo 
"</div>";
?>




ขอบคุณที่แนะนำค่ะ  :wanwan006:
ถ้าเขียนฟอร์ม รับคำแล้วแสดงผล ก็ไม่ยากเท่าไร แต่ควรต้องป้องกันการถูกแฮ็กแบบ XSS (Cross Site Scripting) ด้วยนะครับ
ใช้ htmlentities() ก่อน echo ตัวแปรด้วยครับ

ball6847

<?php

function sanitize_with_dashes($string$length 0)
{
$string preg_replace('~[^a-z0-9ก-๙\s\-]~iu'''$string);

if (is_numeric($length) AND $length AND mb_strlen($string) > $length)
$string mb_substr($string0$length);

$string trim($string);
$string preg_replace('~\s+~''-'$string);
$string strtolower($string);

return $string;
}



We use Ubuntu.

[direct=http://ng-seo.sourcelab.xyz/]AngularJS SEO Experimental[/direct]