By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Regular Expressions

P: n/a
How do I change only the first occurence of a token in a string?

How do I change only the nth occurence of a token in a string?

How do I change the first alpha character after the nth occurence of a
token in a string?
The answer to these questions will allow me to display raw text as
beautiful magazine style layouts using style sheets without manually
altering each document.

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> How do I change only the first occurence of a token in a string?
How do I change only the nth occurence of a token in a string?
How do I change the first alpha character after the nth occurence of a
token in a string?


These functions, either alone (preg-match, less convenient: preg_replace)
or in combination with substr will do.

Find details here:
http://de3.php.net/manual/en/function.substr.php
http://de3.php.net/manual/en/function.preg-match.php
http://de3.php.net/manual/en/function.preg-replace.php

And, please check for the meaning of RTFM.
(one of them is: read the f......riendly manual)
Marek

bn*****@g214mx.net
(remove numbers to despam)
Jul 17 '05 #2

P: n/a
On 8 Feb 2005 09:40:27 -0800
"petrovitch" <we*******@deltafarms.com> wrote:
How do I change only the first occurence of a token in a
string?
Well, only the first occurence is simpler than it seems.

$string = 'lalalalalalalalal';
$p = strpos($string, 'a');

if ($p === false)
exit;

$string{$p} = 'R'; /* replacement letter */

print $string;
How do I change only the nth occurence of a token in a string?
You don't want only the last, right? You want to pass a value
like 5, so you can change the 5th occurrence of a token, right?

I think you can use the same approach above. Consider this
function here. There can be bugs here, but hopefully you get the
idea.

function getpos($s,$c,$n) {

$i = $k = 0;
for ( ;; ) {
$_p=strpos($s, $c, $i);
if( $_p === false) break;

$i = $_p + 1; ++$k; $p = $_p;
}

if( $k < $n ) return -1;

return $p;
}

And then you'd use this way:

$p = getpos('sometimes', 'e', 2);
$string{$p} = 'R'; print $string;

That is, you want the position of the letter 'e' at its second
occurrence. If there isn't a second occurrence, then -1 is
returned.
How do I change the first alpha character after the nth
occurence of a token in a string?
Well, with the function above you get the position of the token,
then you search the original string for the first alpha char
starting from the return of getpos(). It shouldn't be hard, I'll
leave that to you.

In C, there are macros/functions like isspace(), isalpha(), so
you can increment your array index (string index, however PHP
calls it) and at the first time you get an alpha, you get the
position from your index/counter...

Look for something like in PHP or write a function to do it.
Write back if you need further help.
The answer to these questions will allow me to display raw text
as beautiful magazine style layouts using style sheets without
manually altering each document.


I think I realize one problem with my suggestions. What you
called token, I assumed it was a single character... if you need
to look for strings, you will need to adapt the code.

I hope it helps, though.
Jul 17 '05 #3

P: n/a
On 8 Feb 2005 09:40:27 -0800, petrovitch <we*******@deltafarms.com> wrote:
How do I change only the first occurence of a token in a string?

How do I change only the nth occurence of a token in a string?

How do I change the first alpha character after the nth occurence of a
token in a string?
The answer to these questions will allow me to display raw text as
beautiful magazine style layouts using style sheets without manually
altering each document.


Change first occurance:
preg_replace("/test/","cake",$text,1);
Use the limit parameter to do just one replace.

Change the 3rd occurance + next character. It isn't nice, but works.
preg_replace("/((?:.*?test.*?){2})test(\s*)(\w)(.*)/s","\\1cake\\2<span>\\3</span>\\4",$text);
Passes two occurances, to replace the third.
Puts span tags around the next alpha char.

Hope this helps.

--
Stian
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.