Welcome Guest, Not a member yet? Register   Sign In
php preg_replace function
#11

[eluser]sophistry[/eluser]
did you assess the php manual page for the preg_replace() function and find it lacking?

let me give it a try re-writing the manual: the $1 refers to the item captured in the first parenthesized expression. when it is placed in the replacement string it will be replaced by whatever is captured in the pattern string.

so, in this case, you have no parentheses in your regular expression so there is no $1 substitution! that's why you "see" the empty string.

try putting parentheses around the .* (dot-star) regex and see what happens.

how did i do? was that clearer than the manual page? :-)
#12

[eluser]basty_dread[/eluser]
here is what i did
Code:
$string = "the quick brown brown fox jumps over the lazy dog, Brown Fox. <h2>The brown Fox</h2>";
$save = preg_replace('/<h.*>(.*)<\/h.*>/ie',"str_replace(' ','-','$1')" , $string);
print_r($save);

it output:

the quick brown brown fox jumps over the lazy dog, Brown Fox. The-brown-Fox

but it removes the <h2> tag? hehe
now i know how to use $1
thanks. your explanation are a lot clearer than the manual..
#13

[eluser]sophistry[/eluser]
great! i love being lucid.

now, put parentheses around the h tags too...
Code:
'/(<h.*>(.*)<\/h.*>)/ie'
if you put new parens (you can even have them nested) you will have $2 and $1, up to $99 if you decide to have 99 parentheses!
#14

[eluser]basty_dread[/eluser]
Code:
$string = "the quick brown brown fox jumps over the lazy dog, Brown Fox. <h2>The Brown Fox</h2>";
$save = preg_replace('/(<h.*>(.*)<\/h.*>)/ie',"str_replace(' ','-','$1')" , $string);
print_r($save);

echo "<br>";
echo preg_replace('/(<h.*>(.*)<\/h.*>)/ie',"str_replace('-',' ','$1')",$save);

i just put this to turn it back to its original. works great.. thank you..
i will experiment more on this..
#15

[eluser]basty_dread[/eluser]
how can i append something on the replacement?

Code:
$appendthis = "<a href='http://codeigniter.com'>";
$string = "the quick brown brown fox jumps over the lazy dog, Brown Fox. <h2>The Brown Fox</h2>";
$save = preg_replace('/(<h.*>(.*)<\/h.*>)/ie',$appendthis."str_replace(' ','-','$1')".'</a>' , $string);
print_r($save);

echo "<br>";
echo preg_replace('/(<h.*>(.*)<\/h.*>)/ie',"str_replace('-',' ','$1')",$save);
#16

[eluser]sophistry[/eluser]
could you maybe write out exactly what your requirements are? it seems like you are not exactly sure what you want to do with the string. it would help if you wrote out a "spec" for what you want to do with that string and then maybe there is a tool already to do exactly what you are looking for.
#17

[eluser]basty_dread[/eluser]
Ok here is the spec.
I would like to make the h1-h9 tag, a link. something like that..
Code:
$appendthis = "<a href='http://codeigniter.com'>"; // this is what  i supposed to append after replacing spaces with dash (-)
$string = "the quick brown brown fox jumps over the lazy dog, Brown Fox. <h2>The Brown Fox</h2>";
$save = preg_replace('/(<h.*>(.*)<\/h.*>)/ie',$appendthis."str_replace(' ','-','$1')".'</a>' , $string);
print_r($save);

i got error on line 3.. it seems that it also include my $appendthis on the regex.
#18

[eluser]sophistry[/eluser]
i suggest you load the CI url helper and use the anchor() function to generate the link:

remove the $appendthis and the anchor closing tag. just wrap anchor around like this:
Code:
//pseudocode
"anchor('http://codeigniter.com',str_replace())"
remember, remove the closing </a> tag - you don't need it if you use the anchor() function.
#19

[eluser]basty_dread[/eluser]
i cant post a while ago..
thanks for the help sophistry. but i am still having error on that part.
#20

[eluser]sophistry[/eluser]
and.... your code? i don't see it?




Theme © iAndrew 2016 - Forum software by © MyBB