Jump to content

[SOLVED] preg_replace() manipulation help


Kasuke_Akira

Recommended Posts

I have three options:

 

Stronghold Common

Stronghold Uncommon

Stronghold Rare

 

I want preg_replace() to search for all three, and replace with the same array so that I don't have to make three seperate array options for each entry but I'm not sure what the search string would look like.

Link to comment
Share on other sites

Difficult to read what you're after m8, but I'll try.

 

I'm guessing what you want requires no more than str_replace, which is much faster then preg_replace

 


$string = ' Stronghold Common Stronghold Uncommon Stronghold Rare';

$replace = array(
  'Stronghold Common',
  'Stronghold Uncommon',
  'Stronghold Rare'
}

$replace_with = array(
  '1',
  '2',
  '3'
}

echo str_replace($replace, $replace_with, $string); // output is '1 2 3';

Link to comment
Share on other sites

Hmm..lets try a different approach..lol

 

I want any instances of Stronghold Common, Stronghold Uncommon, Stronghold Rare to be replaced with:

 

$replace_str[0] = '<img src\'images/sth.gif\'>';

 

But I don't want to have to do this:

 

$search_str[0] = '/Stronghold Common/';
$search_str[1] = '/Stronghold Unommon/';
$search_str[2] = '/Stronghold Rare/';

$replace_str[0] = '<img src\'images/sth.gif\'>';
$replace_str[1] = '<img src\'images/sth.gif\'>';
$replace_str[2] = '<img src\'images/sth.gif\'>';

 

I want to do this:

 

$search_str[0] = '/Stronghold [different options]/';


$replace_str[0] = '<img src\'images/sth.gif\'>';

Link to comment
Share on other sites

Just an adaption of janroalds, why wouldn't you want to do the below?

 

<?php

$string = ' Stronghold Common Stronghold Uncommon Stronghold Rare';

$replace = array(
  'Stronghold Common',
  'Stronghold Uncommon',
  'Stronghold Rare'
);

$replace_with = '<img src\'images/sth.gif\'>';

echo str_replace($replace, $replace_with, $string); // output is '1 1 1

?>

Link to comment
Share on other sites

I guess he's saying that there can be a variety of different "Stronghold _____" 's and he wants a preg to pick them up.

 

If so, the question is really whether there are _really_ many strongholds, or an unforseen amout of them. (dynamically generated maybe)

 

Unless my above sentence has some truth into it, you should go with the solution Hell Toupe and I gave you.

Link to comment
Share on other sites

Here's what an example set of arrays looks like.

There are over 100 search strings with corresponding replacement strings:

 

$search_str[0] = '/Time Spiral Common/';
$search_str[1] = '/Time Spiral Unommon/';
$search_str[2] = '/Time Spiral Rare/';

$search_str[3] = '/Stronghold Common/';
$search_str[4] = '/Stronghold Unommon/';
$search_str[5] = '/Stronghold Rare/';

$replace_str[0] = '<img src\'images/tsp c.gif\'>';
$replace_str[1] = '<img src\'images/tsp u.gif\'>';
$replace_str[2] = '<img src\'images/tsp r.gif\'>';

$replace_str[2] = '<img src\'images/sth.gif\'>';
$replace_str[3] = '<img src\'images/sth.gif\'>';
$replace_str[4] = '<img src\'images/sth.gif\'>';

 

If you notice for the images for TS, they vary based on Rare, Uncommon, or Common

 

However, there are no different images for each rarity of Stronghold, the same image has to be used.  but I don't want to have to define 3 different search and image arrays, for the same image.

 

I know you can do stuff like this : [0-9] and it will search for anything between 0 and 9 instead of having to do a separate instance in the array for each number.  but I need the same thing to be done with common uncommon and rare so that Stronghold is constant, but one of those other three can be true as well:

 

$search_str[3] = '/Stronghold [Common,Uncommon,Rare]/';

 

I know that isn't how it works, but I think you may understand what I mean now.

Link to comment
Share on other sites

Yes,

 

you can do like this in your preg /stronghold (this|that|other)/

which will match "stronghold this" "stronghold that" or "stronghold other"

 

also you can do something like /stronghold (\w+)/

to math "stronghold whatsoever"

 

In both cases you can return and replace either the entire string or just the subpattern with what suits you. If you are quite clever you can make it so that the relation between the string and the image to put in are consistent so that the right picture can be put in for all circumstances.

 

http://www.php.net/manual/en/reference.pcre.pattern.syntax.php

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.