You are here: Re: can't find the right PCRE pattern for a preg_match_all « PHP Programming Language « IT news, forums, messages
Re: can't find the right PCRE pattern for a preg_match_all

Posted by greatprovider on 12/18/12 11:49

the formulas are inputted into a database in the format:
"Na**3C**6H**5O**7*2H**20"
i query the database and pass the list of unformated formulas through
this function in a loop.

function format_replace($formula) {
$search = "/(\*{2}[\d]+)/";
$replace = "<sub>\l</sub>";
$formatted = preg_replace( $search, $replace, $formula);
return $formatted;
}
the result i get for this is: Na\lC\lH\lO\l*2H\l (where "\l" is
subscripted).

alternatively i wrote this function using preg_match_all(), you may
notice a few other patterns i have commented out, these too failed my
litmus test so to speak.

function format_formula($formula) {
//$search = array ( '/\*{2}[^\d+]*?[\d+\-\s\d+]/' ) -----
((\*{2})[^\d+]*?([\d+])) -----;
//preg_match_all('/(\*\*((\d+)|(\d+\w)|(\d+\s)))/', $formula,
$my_arr); // get the 2-12 subscripted "/(\*{2}([\d]+[\-][\d]+))/"
preg_match_all('/\*{2}[\d]+/', $formula, $my_arr);
var_dump($my_arr);
foreach($my_arr as $key => $val) {
if ($val == NULL || $val == '') {
return;
} else {
for ($i = 0; $i < sizeof($my_arr); $i++) {
for ($j = 0; $j < sizeof($my_arr[$i]); $j++) {
$fixed[$i][$j] = trim($my_arr[$i][$j], "**");
$fixedstr = "<sub>" . $fixed[$i][$j] . "</sub>";
$formatted = str_replace($my_arr[$i][$j], $fixedstr , $formula);

return $formatted;
}
}
}
}
}




Chung Leong wrote:
> greatprovider wrote:
> > i'm starting with a string such as "Na**3C**6H**5O**7*2H**20"
> >
> > im attempting to match all **\d+ ...once i can match all the double
> > asterix \d i intend to wrap the \d in "<sub>" tags for display
> > purposes.
> >
> > i have been trying to write the correct pattern for 2 weeks now without
> > any success...i can get preg_replace() to work, with several simple
> > patterns but when i use preg_match_all, i either get unintended results
> > or incomplete matches.
> >
> > any assistance in matching all "\*{2}\d+" from the above string would
> > be appreciated.
>
> Why do you need preg_match_all for what you're trying to do?
> preg_replace() should be adequate. If not that,
> preg_replace_callback().
>
> I am guessing here, but perhaps you're forgetting to capture the
> digits?
>
> echo preg_replace('/\*{2}(\d+)/', '<sub>\1</sub>',
> "Na**3C**6H**5O**7*2H**20");
>
> Appears to produce a correct representation of Sodium Citrate.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация