Jump to content

replace string function


Destramic

Recommended Posts

this is a function which is apart of my form validation script...basically i want to replace a selected
<label.... and change the class (class="error")..i think i have the script sorted....just the regular expression if someone could please help?

the text inbetween the <label></label> tags in undecided and i want to copy that value into the relaced script....hope that explains what i need that destramic

[code]
    public function mark_label($input_name)
    {
$filename = $_SERVER['SCRIPT_NAME'];
   
ob_start();
require_once $document_root . $filename;
$contents = ob_get_contents();
ob_end_clean();

        $contents = str_replace("<label for=\"$input_name\">(.*)</label>", "<label for=\"$input_name\" class=\"\"></label>", $contents);
return $contents;
    }
[/code]
Link to comment
https://forums.phpfreaks.com/topic/31278-replace-string-function/
Share on other sites

I would use preg_replace() since str_replace() doesn't support regular expressions. Try something like this:
[code]
<?php
$regex = "|(<label for\=\"$input_name\")(>)(.+?)(</label>)|";
$replace = '$1 class="error"$2$3$4';
$contents = preg_replace($regex, $replace, $contents);
?>
[/code]

I just tested it with some test data, and it seems to work just fine.
thanks ive used what you have given me and changed it a bit...but i doesnt seem to work
can you have a look please?

[code]
<?php

$contents = "<label for=\"test\">hello</label><input type=\"text\">";
$regex  = "|<label for=\"test\">(.+?)</label>|";
$replace = "<label for=\"test\" class=\"error\">$1</label>";

$contents = preg_replace($regex, $replace, $contents);
return $contents;

?>
[/code]
[quote author=Destramic link=topic=119293.msg489155#msg489155 date=1166647747]
thanks ive used what you have given me and changed it a bit...but i doesnt seem to work
can you have a look please?

[code]
<?php

$contents = "<label for=\"test\">hello</label><input type=\"text\">";
$regex  = "|<label for=\"test\">(.+?)</label>|";
$replace = "<label for=\"test\" class=\"error\">$1</label>";

$contents = preg_replace($regex, $replace, $contents);
return $contents;

?>
[/code]
[/quote]

Try escaping your equal signs.
ok getting it to replace a string in a var works....but include the current form and changing a label doesnt seem to work...

content:

<label for"name">Name:</label>

[code]
$uri = $_SERVER['REQUEST_URI'];
   
ob_start();
require_once $document_root . $uri;
$contents = ob_get_contents();
ob_end_clean();

$regex  = "|<label for=\"name\">(.+?)</label>|";
$replace = "<label for=\"name\" class=\"error\">$1</label>";

$contents = preg_replace($regex, $replace, $contents);
return $contents;
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.