You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter中如何从URL获取含斜杠的发票号参数?

How to Extract the Full Invoice Number from Your URL

Hey there! I get it—those slashes are messing with your ability to grab the full invoice number (like CN/00003 or CN-00003) from your URL. Let's break down a couple of reliable solutions that handle both formats seamlessly.

Solution 1: Use Regular Expressions (Most Flexible)

Regex is perfect here because it can match both slash-separated (CN/00003) and hyphen-separated (CN-00003) invoice numbers in one go, no matter where they sit in the URL.

Example in JavaScript:

const url = "http://localhost/demo/company/warehouse_subportal/printEway/CN/00003";
// Regex to match "CN" followed by either / or -, then any sequence of digits
const invoiceRegex = /CN[\/-]\d+/;
const matchResult = url.match(invoiceRegex);

// Extract the full invoice number if a match is found
const fullInvoiceNumber = matchResult ? matchResult[0] : "No invoice number found";
console.log(fullInvoiceNumber); // Outputs: CN/00003

Example in Python:

import re

url = "http://localhost/demo/company/warehouse_subportal/printEway/CN-00003"
invoice_regex = r"CN[\/-]\d+"
match_result = re.search(invoice_regex, url)

full_invoice_number = match_result.group(0) if match_result else "No invoice number found"
print(full_invoice_number) # Outputs: CN-00003

If your invoice prefix isn't always CN, tweak the regex to be more general—like r"[A-Z]{2}[\/-]\d+" to match any two uppercase letters followed by a separator and digits.

Solution 2: Split the URL Path (For Consistent URL Structures)

If you know the invoice number is always the last one or two segments of the URL path, splitting the path and combining relevant parts works like a charm.

Example in JavaScript:

const url = new URL("http://localhost/demo/company/warehouse_subportal/printEway/CN/00003");
// Split the path into segments and filter out empty strings from leading/trailing slashes
const pathSegments = url.pathname.split("/").filter(seg => seg !== "");

let fullInvoiceNumber;
const lastSegment = pathSegments[pathSegments.length - 1];
const secondLastSegment = pathSegments[pathSegments.length - 2];

// Check if the second-last segment is the invoice prefix (like CN)
if (secondLastSegment === "CN") {
  fullInvoiceNumber = `${secondLastSegment}/${lastSegment}`;
} else if (lastSegment.startsWith("CN-")) {
  // Handle the hyphen-separated invoice number case
  fullInvoiceNumber = lastSegment;
}

console.log(fullInvoiceNumber); // Outputs: CN/00003

This method is ideal if your URL structure never changes—just adjust the logic based on how your invoice numbers are embedded in the path.


内容的提问来源于stack exchange,提问作者itsme

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:19:10