Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preg challenge 2

Status
Not open for further replies.

elck

Programmer
Apr 19, 2004
176
NL
Hi All,
I have an interesting preg_replace challenge for you.

In my code I have something like this:

[MARK:C:100:120:164:178:T:G:name]

And I'd like to replace that with:
<a href=# onclick=marker('[MARK:C:120:164:178:T:G:name]')>name</a>

The '[MARK:' part and the ']' are constant,
all other variables are variable.

 
That's nowhere near enough to build a deterministic regular expression on.


What are the variables that can appear in the string? Is there any rationale to their ordering? Will the other values be either single letters or multidigit numbers?

Is there any rationale to where the name appears in the string? For example, will it always appear at the end?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
elck-

Take a look at the strrchr() function.

You could use it like this:

Code:
<?php
$info = '[MARK:C:100:120:164:178:T:G:name]';
echo '<a href="#" onclick="';
echo 'marker("' . $info . '")';
echo '">' . strrchr($info, ":") . '</a>';
?>

I haven't tested the code...but it should get you started.

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
Sorry Sleipnir, yes the name is always last.

Very interesting clflava! Just chopping off the last ] should do the trick.

But If anyone has the preg-replace option, I am still interested!

 
I'm curious -

Why would you want to run a preg_replace() on this string? It's clear that you're not actually replacing anything, but rather adding some html to the left and right of the string. This is more of a string concatenation concern than a replace concern.

Am I right?

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
Building a regex involves investigation and pattern recognition. As sleipnir214 states, there's not enough information here.
Can you abstract the string?
Like [MARK followed by : one upper case letter : 1-3 digits etc. ?
Once you get there you can build an expression and we'll be able to help you.
 
I see what you mean cFLava,
of course, if the code does the trick then the method doesn't matter.
But the line is part of a function that replaces a lot of things in some user-entered text, and most other replacements are done with preg_replace.
Therefor it will make it easier to understand the code later, not as if suddenly something else is going on.

More importantly, I am an oldfashioned Basic programmer, and I used to do this with instr and mid$, but I want to understand regular expression better, as I think they are elegant, mysterious, but elegant.

 
Wait a sec clflava!
As I didn't get any suggestions on how to do it with preg,
I thought, 'well, I'll take clflava's suggestion, but the problem of course is that I firstly have to find the [MARK:xx:xxx:xx:xxxx:name] part!, which is easy with preg and hard and sloppy with substr.
There must be ways to find the LAST occurence of : within the [MARK ... ] ?
In your example $info = " a lot of text [MARK..] more text.
Now do you see why I am better off with preg_ ?
 
Here's a solution using a regular expression:
Code:
<?php
$str = "Whatever text you have on a line [MARK:C:120:164:178:T:G:name] and other foo stuff here";
# matching example
preg_match('/(\[MARK:.+:.+:.+:.+:(.+)\])/',$str,$myArr);
$newStrA= '<a href="#" onclick="marker(\''.$myArr[1].'\')">'.$myArr[2].'</a>';
# replacement example
$newStrB = preg_replace('/(.+)(\[MARK:.+:.+:.+:.+:(.+)\])(.+)/',"$1 <a href=\"#\" onclick=\"marker('$2')\">$3</a>$4",$str);
echo(htmlentities($newStr));
?>

Let me know if you want an explanation of the pattern syntax.
 
Thanks all,
One day I will be able to figure this out myself,
until that time, thank you!
 
If all one needs is the the name, and if the name is guaranteed to be last, a regular expression isn't necessary.

On my machine, the script:
Code:
<?php
$a = '[MARK:C:100:120:164:178:T:G:foobarbaz]';

$a_array = explode (':', $a);

$name = str_replace (']', '', end($a_array));

print '<a href=# onclick="marker(\'' . $a  . '\')">' . $name . '</a>';
?>

outputs:

<a href=# onclick="marker('[MARK:C:100:120:164:178:T:G:foobarbaz]')">foobarbaz</a>


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Yeah , yeah, but $a = "something or other [Mark:x:y:a:name] something else"

 
elck:
elck said:
Yeah , yeah, but $a = "something or other [Mark:x:y:a:name] something else"
but.....
elck said:
In my code I have something like this:

[MARK:C:100:120:164:178:T:G:name]

And I'd like to replace that with:
<a href=# onclick=marker('[MARK:C:120:164:178:T:G:name]')>name</a>




Then you'll need one preg_match() (as DRJ478 indicated in his code) to find the marker. But still, after that I recommend not using regular expressions everywhere. They're resource intensive.

Code:
<?php
$original = 'The rain in Spain stays [MARK:C:100:120:164:178:T:G:foobarbaz] mainly in the plain';

preg_match ('/\[MARK:[^\[]+\]/', $original, $a);

$a_array = explode (':', $a[0]);

$name = str_replace (']', '', end($a_array));

print '<a href=# onclick="marker(\'' . $a[0]  . '\')">' . $name . '</a>';
?>


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Should I have said 'within' my code?

I'm sorry, Sleipnir, English is not my first language.

Again, thanks everyone for your contributions!
 
The replacement example that I provided resolves the finding and substitution in one line. I just provided the matching example to show preg_match(). The preg_replace() example eliminates the need for further manipulation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top