Regular Expressions

This is a discussion on Regular Expressions within the PHP Language forums, part of the PHP Programming Forums category; How do I change only the first occurence of a token in a string? How do I change only the ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-08-2005
petrovitch
 
Posts: n/a
Default Regular Expressions

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.

Reply With Quote
  #2 (permalink)  
Old 02-08-2005
Marek Möhling
 
Posts: n/a
Default Re: Regular Expressions

> 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

bn548mm@g214mx.net
(remove numbers to despam)


Reply With Quote
  #3 (permalink)  
Old 02-08-2005
Kevin
 
Posts: n/a
Default Re: Regular Expressions

On 8 Feb 2005 09:40:27 -0800
"petrovitch" <webmaster@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.
Reply With Quote
  #4 (permalink)  
Old 02-08-2005
Stian Berger
 
Posts: n/a
Default Re: Regular Expressions

On 8 Feb 2005 09:40:27 -0800, petrovitch <webmaster@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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:11 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0