PHP yordamida HTML satrida atribut qiymati o'zgaradi

HTML qatoridan o'tgan filtr kancasi bor. Masalan, quyidagicha bo'lishi mumkin:

'<input type="text" value="4893" />'

Qator filtr kancasiga o'tkaziladi:

add_filter( 'html_filter', 'my_html_filter', 10, 1 );
function my_html_filter( $html ) {

    $html =     <--- REPLACE VALUE ATTRIBUTE HERE

    return $html;
}

my_html_filter() ichida qilishim kerak bo'lgan narsa value = "" qiymatini o'zgartiradi va uni $ html . Tasodifiy misol sifatida $ html deb aytiladi:

'<input type="text" value="345" />'

va uni quyidagicha o'zgartirish kerak:

'<input type="text" value="14972" />'

Qanday qilaman? Str_replace va regex ifodasi kombinatsiyasi?

1
Bu, ehtimol, ishlaydi, lekin regex yordamida HTML-da hech qachon yaxshi fikr emas. Faqatgina qiymatni o'zgartiradigan boshqa kanca bo'lishi mumkin, siz qanday baho berayotganimizni baham ko'rasizmi?
qo'shib qo'ydi muallif Thomas Moors, manba
Bu, ehtimol, ishlaydi, lekin regex yordamida HTML-da hech qachon yaxshi fikr emas. Faqatgina qiymatni o'zgartiradigan boshqa kanca bo'lishi mumkin, siz qanday baho berayotganimizni baham ko'rasizmi?
qo'shib qo'ydi muallif Thomas Moors, manba
qo'shib qo'ydi muallif splash58, manba
qo'shib qo'ydi muallif splash58, manba
Javoblardan biri foydali bo'ldimi? Agar shunday bo'lsa, iltimos, upvote va qabul qilingan deb belgilang yoki qayta aloqa bering. Rahmat!
qo'shib qo'ydi muallif miken32, manba
Javoblardan biri foydali bo'ldimi? Agar shunday bo'lsa, iltimos, upvote va qabul qilingan deb belgilang yoki qayta aloqa bering. Rahmat!
qo'shib qo'ydi muallif miken32, manba

6 javoblar

HTMLni tahlil qilish uchun HTML-parserdan foydalaning!

$html = '<input type="text" value="4893" />';
$dom = new DomDocument;
$dom->loadHTML($html);
$nodes = $dom->getElementsByTagName('input');
$node = $nodes[0];
$node->setAttribute('value', 'foo');
echo $dom->saveHTML($node);

Natija:

<input type="text" value="foo">
1
qo'shib qo'ydi

HTMLni tahlil qilish uchun HTML-parserdan foydalaning!

$html = '<input type="text" value="4893" />';
$dom = new DomDocument;
$dom->loadHTML($html);
$nodes = $dom->getElementsByTagName('input');
$node = $nodes[0];
$node->setAttribute('value', 'foo');
echo $dom->saveHTML($node);

Natija:

<input type="text" value="foo">
1
qo'shib qo'ydi

HTMLni tahlil qilish uchun HTML-parserdan foydalaning!

$html = '<input type="text" value="4893" />';
$dom = new DomDocument;
$dom->loadHTML($html);
$nodes = $dom->getElementsByTagName('input');
$node = $nodes[0];
$node->setAttribute('value', 'foo');
echo $dom->saveHTML($node);

Natija:

<input type="text" value="foo">
1
qo'shib qo'ydi
add_filter( 'html_filter', 'my_html_filter', 10, 1 );
function my_html_filter( $html ) {

   //assuming you have a way to know the new value you want
   //i used an example value, probably you have to construct 
   //the following string
    $new_value = 'value="65432"';

    $html = preg_replace('/value="[0-9]+"/', $new_value, $html);

    return $html;
}
0
qo'shib qo'ydi
add_filter( 'html_filter', 'my_html_filter', 10, 1 );
function my_html_filter( $html ) {

   //assuming you have a way to know the new value you want
   //i used an example value, probably you have to construct 
   //the following string
    $new_value = 'value="65432"';

    $html = preg_replace('/value="[0-9]+"/', $new_value, $html);

    return $html;
}
0
qo'shib qo'ydi
add_filter( 'html_filter', 'my_html_filter', 10, 1 );
function my_html_filter( $html ) {

   //assuming you have a way to know the new value you want
   //i used an example value, probably you have to construct 
   //the following string
    $new_value = 'value="65432"';

    $html = preg_replace('/value="[0-9]+"/', $new_value, $html);

    return $html;
}
0
qo'shib qo'ydi
PhP |BotsUz
PhP |BotsUz
93 ishtirokchilar

Phpni o'rganishni Hohlasangiz https://t.me/joinchat/AAAAAE-KRc5dd5tPMmGmWA A'zo bo'lin