Jump to content

Recommended Posts

I need to search a string for certain substrings, here are some examples of substrings i'd like to find in my string:

{$content}
{$title}
{$moduleLeft}
{$moduleRight}

so i created a pattern:

{$[A-Z|a-z]+}

then recognized i had to escape some chars ..correct?^^

\{\$[A-Z|a-z]+\}

It doesn't work properly but just finds the first occurence - im using the function ereg:

ereg('{\$[A-Z|a-z]+}', $content, $positions);

The array $positions should store all of thoose substrings.

But $positions always consists of only 1 element. ($positions[1] is always set, no other elements are set)

If i delete the first match in $content, the value of $positions[1] changes accordingly - which is why i'm not sure whether regular expression or something else is incorrect..

 

I'll appreciate any help  :)

 

Link to comment
https://forums.phpfreaks.com/topic/165711-solved-regular-expression/
Share on other sites

Do you mean this ?

<?php
$content = '{$content}
{$title}
{$moduleLeft}
{$moduleRight}';
preg_match_all('/\{\$[a-z]+\}/i', $content, $result);
foreach($result[0] as $Item)
{
echo $Item;
}
?>

 

if you want just the word ie title

then

change the RegEx to

'/\{\$([a-z]+)\}/i'

and

$result[0] to $result[1]

Okay.. my example input was wrong.. did you test it with your input.. and was the result what you expected ?

 

EDIT: would this be the correct response

{$title}{$content}

to your input of

$content = '<html><head><title>{$title}</title></head><body>{$content}</body></html>';

$content = "<html><head><title>{$title}</title></head><body>{$content}</body></html>";
ereg('{\$[A-Z|a-z]+}', $content, $positions);

 

after this the array $positions should consist of 2 elements:

$positions[1] == "{$title}"
$positions[2] == "{$content}"

 

..but for some strange reason ereg only stores the first occurrence (in this case "{$title}") in the array.. what do i have to change to store all occurrences in $positions?

Like this (remember arrays start from 0 not 1)

<?php
$content = '<html><head><title>{$title}</title></head><body>{$content}</body></html>';
preg_match_all('/\{\$[a-z]+\}/i', $content, $positions);
$positions =$positions[0];
echo $positions[0];
echo $positions[1];
?>

even if i've got more than 2 matches only the first one is strored

the first index of the resulting array is 1 ..take a look at function ereg

 

If matches are found for parenthesized substrings of pattern and the function is called with the third argument regs , the matches will be stored in the elements of the array regs .

 

    $regs[1] will contain the substring which starts at the first left parenthesis; $regs[2] will contain the substring starting at the second, and so on. $regs[0] will contain a copy of the complete string matched.

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.