PDA

View Full Version : کمک در جایگزینی رشته



foad1992
یک شنبه 05 دی 1389, 10:06 صبح
سلام دوستان

$a='<img src"" width="200"/>';
در اینجا میخوام جای width="200" یک مقدار دیگه بذارم مثلا width="500"
ممنون

hossin.esm
یک شنبه 05 دی 1389, 11:36 صبح
<?php
$a='<img src"" width="200"/>';
$a = str_replace("width=\"200\"", "width=\"500\"", $a);
echo $a;
?>

foad1992
یک شنبه 05 دی 1389, 14:28 عصر
<?php
$a='<img src"" width="200"/>';
$a = str_replace("width=\"200\"", "width=\"500\"", $a);
echo $a;
?>
اگر در width="200" عدد رو ندونم راهی است که بشه عدد رو بدست اورد
یا
از width به بعد کد رو پاک کنم

funpatogh
یک شنبه 05 دی 1389, 20:05 عصر
<?php
$a='<img src"" width="200"/>';
$a=preg_match('/width="([0-9]+)"/',$a,$x);
echo($x[1]);
?>

Vahid Faraji
یک شنبه 05 دی 1389, 20:59 عصر
<?php
$a='<img src"" width="200"/>';
$a=preg_match('/width="([0-9]+)"/',$a,$x);
echo($x[1]);
?>





int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )


matches:
If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.
پس در مثال بالا،
$x[1] به پترن داخل پرانتز اشاره کرده و 200 را بر می گرداند.

foad1992
دوشنبه 06 دی 1389, 00:40 صبح
ممنون از کمک
ولی یه مشکل برخوردم حالا می خوام عدد رو جایگزین عدد width کنم کار نمی کنه
اینم کد


$a=" <img src=\"1.jpg\" width=\"500\" height=\"153\">";
$a=preg_match('/width="([0-9]+)"/',$a,$x);
$m=$x[1];
echo str_replace("$m","100",$a);