In ACF, the_field('battery'); outputs as follows :

Battery : Sony Rechargeable Ni-MH 2200 mAh battery


Battery : Energizer AA Rechargeable Batteries 2300 mAh NiMH 

How can i achieve this (showing only mAh details) :

Battery : 2200 mAh


Battery : 2300 mAh

You have to use a regex, in this case, to extract the needed value from a string:

$string = 'Energizer AA Rechargeable Batteries 2300 mAh NiMH';

preg_match('/([0-9]+\s*mah)/i', $string, $capacity );
if ( isset( $capacity[0] ) ) {
    printf('Battery: %s', $capacity[0] );

This regex /([0-9]+\s*mah)/i will look for any numbers followed by case insensitive mah combination.

You can test this regex online here https://regex101.com/r/KDdHbp/1/

Also, here is documentation on preg_match https://www.php.net/manual/en/function.preg-match.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.