Jump to content

replace text problem


mridang_agarwal

Recommended Posts

Hi everyone,
I'm new here and this is is my first time on a PHP forum. I have a small problem which i hope you can help me out with. Its like this:

I have a paragraph of text.

[b][u]<this>Lorem ipsum</this>[/u] dolor sit amet, consectetuer adipiscing elit. Vestibulum eleifend semper elit. Proin pellentesque nonummy nulla. Class aptent [u]<this>taciti sociosqu</this> [/u]ad litora torquent per conubia nostra, per inceptos hymenaeos. Curabitur nec pede eu sem tempor iaculis. Suspendisse Lorem ipsum suscipit, metus quis molestie Lorem ipsum acinia, erat ipsum pellentesque arcu, vitae porta [u]<this>mauris tellus</this>[/u] non dolor. Vivamus nec ligula. Ut sit amet arcu ac metus pretium facilisis. Pellentesque[/b]


Now, I would like to replace all the occurence of <this>content_here</this> with anything (any random text). The i would do some string formatting on the paragraph and then replace all the replaced text with the original text.

Example, in the above paragraph i repace all the occureces with some random text making it look like this.

[b][u]:stuff3435:[/u] dolor sit amet, consectetuer adipiscing elit. Vestibulum eleifend semper elit. Proin pellentesque nonummy nulla. Class aptent [u]:stuff6634:[/u]ad litora torquent per conubia nostra, per inceptos hymenaeos. Curabitur nec pede eu sem tempor iaculis. Suspendisse Lorem ipsum suscipit, metus quis molestie Lorem ipsum acinia, erat ipsum pellentesque arcu, vitae porta [u]:stuff9921:[/u] non dolor. Vivamus nec ligula. Ut sit amet arcu ac metus pretium facilisis. Pellentesque[/b]

Now, as you can see, i have rep;aced all the occurences of <this>content_here</this> with ':stuffXXXX:' (where XXXX is a random number. After I have done the formatting which I need i would again replace ':stuffXXXX:'  back with the original text. Lets assume that during my string formatting, no occurences of ':stuffXXXX:' are altered.

How do i do this? could someone give me an example? Thanks in advace guys.
Link to comment
https://forums.phpfreaks.com/topic/25784-replace-text-problem/
Share on other sites

I thort you wanted to randomly say somethink each time thepage was opened here you go.

[code]

<?php
$numbers =array ('Look at this','This looks good','This is grate');

shuffle($numbers);

for($i=0; $i<1; $i++) {
$x= "$numbers[$i]";
}

echo "Hi there $x hope you enjoy";
?>

[/code]
Link to comment
https://forums.phpfreaks.com/topic/25784-replace-text-problem/#findComment-117752
Share on other sites

hey Orio,
I know about preg_replace but the question is in the paragraph once i repace the text within the 'text' quotes how do i repace them back. I know for a fact that i would have to use preg_replace once again but how? I wouldnt know what to replace?
Link to comment
https://forums.phpfreaks.com/topic/25784-replace-text-problem/#findComment-117771
Share on other sites

In that case, you need first to find and store all of the <this>something</this> and then replace:

[code]<?php

$text = "<this>Lorem ipsum</this> dolor...";
$random = ":stuff6634:";
echo "<h3>Before-</h3>".htmlentities($text);

$regex = "/<this>.*?<\/this>/";
preg_match_all($regex,$text,$matches);
$text = preg_replace($regex, $random, $text);
echo "<br><br><br><h3>After1-</h3>".htmlentities($text);

foreach ($matches[0] as $val)
  $text = preg_replace($random,$val,$text,1);
echo "<br><br><br><h3>After2-</h3>".htmlentities($text);

?>[/code]

Orio.
Link to comment
https://forums.phpfreaks.com/topic/25784-replace-text-problem/#findComment-117802
Share on other sites

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.