7 Replies - 2717 Views - Last Post: 24 March 2009 - 04:15 PM Rate Topic: -----

#1 JakobJ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-March 09

Match and replace text?

Post icon  Posted 22 March 2009 - 06:30 AM

Hello there,

I'm stuck again now, so I decided to post it here to see if anyone could help me ;).

I've got a string that looks similar this:
[data]Data 1|Data 2|Data 3|Data 4[/data]
[data]Data 1|Data 2|Data 3|Data 4[/data]
[data]Data 1|Data 2|Data 3|Data 4[/data]
[data]Data 1|Data 2|Data 3|Data 4[/data]
[data]Data 1|Data 2|Data 3|Data 4[/data]
[data]Data 1|Data 2|Data 3|Data 4[/data]


The number of [data]-"tags" is varying, it can be 4 and it can eb over 100.
So, what do I want to do? I want to insert this to a tooltip.
My current code looks like this:
function tooltip($text) {

	$matches = preg_match_all("/\[data\](.*?)\[\/data\]/is", $text);
	foreach($matches as $match) {
		$data = explode("||", $match);
		$name = $data[0];
		$info = $data[1];
		$file = $data[2];
		$folder = $data[3];
		$replace = "<li onmouseover=\"Tip('<img src=\'images/".$folder."/".$file."\'><br />".$info."', TITLE, '".$name."')\" onmouseout=\"UnTip()\"><img src=\'images/".$folder."/".$file."\'></div>";
		$newtext = preg_replace($pattern, $match, $text);
		return $newtext;
	}
}


So, short version:
Something that, multiple times, replaces:
[data]Data 1|Data 2|Data 3|Data 4[/data]


With:
<li onmouseover="Tip('<img src=\'images/Data 4/Data 3\'><br />Data 2', TITLE, 'Data 1')\" onmouseout=\"UnTip()\"><img src=\'images/Data 4/Data 3\'></div>


This post has been edited by JakobJ: 22 March 2009 - 07:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Match and replace text?

#2 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Match and replace text?

Posted 22 March 2009 - 11:17 AM

Try using str_replace as opposed to preg_replace
Was This Post Helpful? 0
  • +
  • -

#3 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Match and replace text?

Posted 22 March 2009 - 05:11 PM

You're assigning the $matches variable wrong. preg_match_all() returns the number of matches made. The third argument is where $matches should be assigned:

preg_match_all("/\[data\](.*?)\[\/data\]/is", $text, $matches);
Was This Post Helpful? 0
  • +
  • -

#4 JakobJ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-March 09

Re: Match and replace text?

Posted 23 March 2009 - 06:47 AM

Thank you for the responds! :)

I changed the code a bit (after what you wrote, Valek):
function furnitooltip($text) {

	preg_match_all("/\[data\](.*?)\[\/data\]/is", $text, $matches);

	foreach($matches as $match) {
		$data = explode("||", $match);
		$name = $data[0];
		$info = $data[1];
		$file = $data[2];
		$folder = $data[3];
		$replace = "<li onmouseover=\"Tip('<img src=\'images/".$folder."/".$file."\'><br />".$info."', TITLE, '".$name."')\" onmouseout=\"UnTip()\"><img src=\'images/".$folder."/".$file."\'></li>";
		return preg_replace($match, $replace, $text);
	}
}


Now it warns:
Warning: preg_replace() [function.preg-replace]: Unknown modifier 'l'

Ideas?

This post has been edited by JakobJ: 23 March 2009 - 06:48 AM

Was This Post Helpful? 0
  • +
  • -

#5 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Match and replace text?

Posted 23 March 2009 - 02:43 PM

In regular expressions, the pipe (|) has special meaning. If it's in your results, you'll have to escape it with a backslash (\).

Also, I notice you're running explode() with a double pipe as the separator, but the original input (unless you modified it) has single pipes.

This post has been edited by Valek: 23 March 2009 - 02:43 PM

Was This Post Helpful? 0
  • +
  • -

#6 JakobJ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-March 09

Re: Match and replace text?

Posted 24 March 2009 - 06:50 AM

Oh, must have forgotten to add the second pipe in the example string, I'm using two of them (||) to separate, sorry if it confused you. :P


Anyway, am I supposed to write \|\| instead of just || in the string, then?
And I did a little test:
function tooltip($text) {

	preg_match_all("/\[data\](.*?)\[\/data\]/is", $text, $matches);

	foreach($matches as $match) {
		echo $match;
	}
}


Outputs:

Quote

ArrayArray

No matter if the sting doesn't contain any [data]-tags, or if it contains hundreds of them...
Was This Post Helpful? 0
  • +
  • -

#7 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: Match and replace text?

Posted 24 March 2009 - 02:00 PM

you're outputting raw Array data. If you want to view whats in the array, you'll need to use print_r()
Was This Post Helpful? 0
  • +
  • -

#8 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Match and replace text?

Posted 24 March 2009 - 04:15 PM

Judging by what you're aiming to do, you'll want to manipulate the $matches[1] array. $matches[0] contains all matches including the data tags. $matches[1] just has the data within the tags.

So you'd want to do foreach($matches[1] as $match) :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1