PHP RegExp String Conversion
August 7, 2025 ยท AI Conversations
### โ PHP Code:
<?php
$input = '(J)VGL106871001867753816';
$output = preg_replace('/^((w))/', '$1', $input);
echo $output; // Outputs: JVGL106871001867753816
### ๐ Explanation:
– `^((w))`:
– `^` ensures the match is at the start of the string.
– `(` and `)` match literal parentheses.
– `(w)` captures a single alphanumeric character (like `J`).
– `$1` inserts the captured letter (e.g. `J`) without the parentheses.
Let me know if the pattern varies and you need more flexible matching.
### โ Full Robust Example:
<?php
function normalizeCode($input) {
return preg_replace('/^((w))/', '$1', $input);
}
// Test cases
$examples = [
'(J)VGL106871001867753816',
'JVGL106871001867753816',
'(A)XYZ123456789',
'XYZ123456789'
];
foreach ($examples as $example) {
echo normalizeCode($example) . PHP_EOL;
}
### ๐งช Output:
JVGL106871001867753816
JVGL106871001867753816
AXYZ123456789
XYZ123456789
### โ
Summary:
– If the string starts with a pattern like `(J)` โ it removes the parentheses and merges.
– If the string is already formatted โ no change is made.
Let me know if the prefix could contain more than one character or if you want to sanitize other patterns too.
add_shortcode('flatsome_blog_posts', 'flatsome_blog_category_posts');
function flatsome_blog_category_posts($atts)
{
// Define shortcode attributes with defaults
$atts = shortcode_atts([
'cat_id' => 1126,
'pagination' => 'yes',
'columns' => 3,
'searchbox' => 'yes',
'category_tabs' => 'yes',
], $atts);
ob_start();
$terms = get_terms([
'taxonomy' => 'category',
'hide_empty' => true,
]);
echo $atts['pagination'];
if (!is_wp_error($terms))
{
$first_cat_id = intval($atts['cat_id']);
$args=['post_type' => 'post',
'posts_per_page' => 6,
'paged' => 1];
if($first_cat_id!=0)
$args['cat']=$first_cat_id;
$initial_query = new WP_Query($args);
$template_path = get_stylesheet_directory() . '/partials/blog-post-category-template.php';
if (file_exists($template_path)) {
include $template_path;
} else {
echo 'Template not found.';
}
}
return ob_get_clean();
}
above attributes not receiving
Also: you're echoing `$atts['pagination']` for debugging, which is good, but you should also ensure everything is working as expected with the attributes. Here's the fix:
—
### โ Fix: Make attribute values case-insensitive and debug correctly
add_shortcode('flatsome_blog_posts', 'flatsome_blog_category_posts');
function flatsome_blog_category_posts($atts)
{
// Define shortcode attributes with defaults
$atts = shortcode_atts([
'cat_id' => 1126,
'pagination' => 'yes',
'columns' => 3,
'searchbox' => 'yes',
'category_tabs' => 'yes',
], $atts);
// Normalize values (lowercase for easier comparison)
$pagination = strtolower($atts['pagination']) === 'yes';
$searchbox = strtolower($atts['searchbox']) === 'yes';
$category_tabs = strtolower($atts['category_tabs']) === 'yes';
$cat_id = intval($atts['cat_id']);
$columns = intval($atts['columns']);
ob_start();
$terms = get_terms([
'taxonomy' => 'category',
'hide_empty' => true,
]);
// Debug (optional)
// var_dump($pagination, $searchbox, $category_tabs);
if (!is_wp_error($terms)) {
$args = [
'post_type' => 'post',
'posts_per_page' => 6,
'paged' => 1,
];
if ($cat_id !== 0) {
$args['cat'] = $cat_id;
}
$initial_query = new WP_Query($args);
// Pass values to template if needed
$template_path = get_stylesheet_directory() . '/partials/blog-post-category-template.php';
if (file_exists($template_path)) {
include $template_path;
} else {
echo '<p>Template not found.</p>';
}
}
return ob_get_clean();
}
—
### โ Usage:
[flatsome_blog_posts cat_id="0" pagination="No" searchbox="No" category_tabs="No" columns="2"]
—
### Optional Tip:
You can even pass these normalized values to the template:
include $template_path;
// OR make available:
$GLOBALS['flatsome_blog_settings'] = compact('pagination', 'searchbox', 'category_tabs', 'columns');
Let me know if you want tab/search UI logic in the PHP/HTML template too.
can you also allow how to no pagination is yes or no
—
### โ Step-by-step Plan:
#### 1. **Pass normalized settings (pagination, searchbox, etc.) to the template**
Use `compact()` to bundle them into an array and pass them to the template.
#### 2. **Also make sure to pass them in AJAX (via JS)**
Youโll send `pagination=yes/no` and others to the AJAX handler too.
#### 3. **Inside template** (`blog-post-category-template.php`)
Check these flags like `$settings['pagination']` to show/hide pagination HTML.
—
### โ Updated Shortcode PHP:
add_shortcode('flatsome_blog_posts', 'flatsome_blog_category_posts');
function flatsome_blog_category_posts($atts)
{
$atts = shortcode_atts([
'cat_id' => 1126,
'pagination' => 'yes',
'columns' => 3,
'searchbox' => 'yes',
'category_tabs' => 'yes',
], $atts);
$settings = [
'pagination' => strtolower($atts['pagination']) === 'yes',
'searchbox' => strtolower($atts['searchbox']) === 'yes',
'category_tabs' => strtolower($atts['category_tabs']) === 'yes',
'columns' => intval($atts['columns']),
'cat_id' => intval($atts['cat_id']),
];
ob_start();
$terms = get_terms([
'taxonomy' => 'category',
'hide_empty' => true,
]);
if (!is_wp_error($terms)) {
$args = [
'post_type' => 'post',
'posts_per_page' => 6,
'paged' => 1,
];
if ($settings['cat_id'] !== 0) {
$args['cat'] = $settings['cat_id'];
}
$initial_query = new WP_Query($args);
$template_path = get_stylesheet_directory() . '/partials/blog-post-category-template.php';
if (file_exists($template_path)) {
include $template_path;
} else {
echo '<p>Template not found.</p>';
}
}
return ob_get_clean();
}
—
### โ In `blog-post-category-template.php` Example:
<?php
// Make sure $initial_query and $settings are available
if (!empty($initial_query->posts)) :
foreach ($initial_query->posts as $post) :
setup_postdata($post);
?>
<div class="blog-item"><?php the_title(); ?></div>
<?php
endforeach;
wp_reset_postdata();
else :
echo '<p>No posts found.</p>';
endif;
// Show pagination if enabled
if ($settings['pagination']) :
?>
<div class="ajax-pagination">
<!-- pagination links here -->
</div>
<?php endif; ?>
—
### โ
In JS AJAX call:
Send these values when loading by category:
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'load_blog_posts_by_category',
cat_id: clickedCatId,
pagination: 'yes', // or 'no' based on current setting
searchbox: 'no',
category_tabs: 'no',
},
success: function (response) {
$('#blog-container').html(response);
}
});
—
### โ
In AJAX PHP handler:
Use similar logic:
add_action('wp_ajax_load_blog_posts_by_category', 'load_blog_posts_by_category');
add_action('wp_ajax_nopriv_load_blog_posts_by_category', 'load_blog_posts_by_category');
function load_blog_posts_by_category() {
$settings = [
'pagination' => strtolower($_POST['pagination'] ?? 'yes') === 'yes',
'searchbox' => strtolower($_POST['searchbox'] ?? 'yes') === 'yes',
'category_tabs' => strtolower($_POST['category_tabs'] ?? 'yes') === 'yes',
'columns' => intval($_POST['columns'] ?? 3),
'cat_id' => intval($_POST['cat_id'] ?? 0),
];
$args = [
'post_type' => 'post',
'posts_per_page' => 6,
'paged' => 1,
];
if ($settings['cat_id']) {
$args['cat'] = $settings['cat_id'];
}
$initial_query = new WP_Query($args);
$template_path = get_stylesheet_directory() . '/partials/blog-post-category-template.php';
if (file_exists($template_path)) {
include $template_path;
} else {
echo '<p>Template not found.</p>';
}
wp_die(); // for AJAX
}
—
Let me know if you want the pagination to support page numbers via AJAX (`paged`) as well โ I can include that logic too.